-2
$month = [
   "January",
   "February",
   "March",
   "April",
   "May",
   "June",
   "July",
   "August",
   "September",
   "October",
   "November",
   "December"
];

$month = $_POST['month'];
$year = $_POST['year'];
$day = $_POST['day'];
$dateOfDoc = "$year/$month/$day";
$date_now = date("Y/m/d");

if($dateOfDoc >= $date_now){
    echo"yes u can upload";
} else {
    echo "no u can't upload";
}

I am trying to compare today and the user input date and I should allow user if the user input date should be more than today but I always get false part even user put tomorrow's date example )

today : 2019 04 25
user date : 2019 04 05 return false

today : 2019 04 25
user date : 2019 12 05 return true

what's wrong with my code?

andrew
  • 9
  • 3
  • 2
    Your $dateOfDoc variable is just string, not date. Convert it to date. – ismakv Apr 25 '19 at 08:36
  • Why are you generating any array of month names, only to then overwrite the variable you stored this in right after? What were the actual values of $dateOfDoc and $date_now, when you used `var_dump` to make debug outputs? – 04FS Apr 25 '19 at 08:38
  • Alternatively, `strtotime`, then compare as you would. – Script47 Apr 25 '19 at 08:44
  • @ismakv date is a string finally. if date is proper string everything will be fine. – Rahul Apr 25 '19 at 09:16

3 Answers3

1

You are misusing date format with separator /

$month = $_POST['month'];
$year = $_POST['year'];
$day = $_POST['day'];
$dateOfDoc = "$month/$day/$year";
$date_now = date("m/d/Y");

if($dateOfDoc >= $date_now){
    echo"yes u can upload";
} else {
    echo "no u can't upload";
}

Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

Source link.

Demo.

Rahul
  • 18,271
  • 7
  • 41
  • 60
1

Try this code:

$current_date = date('d/m/y');
$post_date = date("d/m/y", mktime(0, 0, 0, $_POST['day'], $_POST['month'], $_POST['year']));

if (strtotime($post_date) >= strtotime($current_date)) {
 echo 'Upload';
} else {
 echo 'Do not upload';
}
Pintu Kumar
  • 311
  • 1
  • 9
0

You are comparing Text strings with Digits!

if $name contains Name of month you can't compare it with date("m") in php since date("m") in php return digital number of month but you store name of month in your variable $name

all of your parameters to compare should be have equal format

Netlog
  • 137
  • 1
  • 9