-1

I have a php form which requires the user to input a date. I have three check boxes that they can choose from: "today", "tomorrow", and "other" (where other requires them to enter a specific date in the field.

I want "today" to print out the current day, "tomorrow" to print out tomorrow's day/date, and "other" to print out the date they choose. The format should be "Day, Month date" (Friday, July 8).

For today's date it's pretty straight forward, just use the getdate() array, but is there a simple way to get tomorrow's date (without requiring multiple steps/error checking)? And is there a simple way to convert the output of (and is the day of the week stored in the output for type=date)?

djd97
  • 77
  • 1
  • 1
  • 10
  • Check out PHP's [date](http://php.net/manual/en/function.date.php) and [strtotime](http://php.net/manual/en/function.strtotime.php) functions. – mcon Jul 08 '16 at 17:55

3 Answers3

1

You can use strtotime.

Tomorrow: <? $tomorrow = strotime("+1 day"); ?>

For any date you'd need to parse it, with strotime($date_input);, however be aware that PHP tries to find out the date format, with dates separated by slashes being the american standard (M/D/Y), and dates separated by dashes being international (D-M-Y).

For the weekday, it's a parameter (l) to the date function. So to print in the format you wish:

echo date('l, F d', strtotime($date));
asiviero
  • 1,225
  • 10
  • 16
  • So for the other date field I did this: "$date=strtotime($_POST['pickupDateOther']);" and when I inputted the date (03-14-2017) it shows: "1489464000" – djd97 Jul 08 '16 at 18:45
  • This is the timestamp of the date, to be passed as the second parameter to `date`. The string in the format you wish is obtained by `$dateFormatted = date('l, F d', $date);` – asiviero Jul 08 '16 at 18:55
  • Sorry, I had missed that your `$date` already has a `strtotime`. No need to call it again when formatting – asiviero Jul 08 '16 at 18:58
  • It still prints out a weird number. Is there something I'm forgetting to do? – djd97 Jul 08 '16 at 19:26
  • `$date=strtotime($_POST['pickupDateOther']); $dateFormatted = date('l, F d', $date); echo $dateFormatted;` ? – asiviero Jul 08 '16 at 19:35
  • Sorry for the late reply, I've been busy the last few days and haven't had a chance to work on the site. I have this but nothing is printed. $date = strtotime($_POST['pickupDateOther']); $dateFormatted = date('l, F d', $date); @asiviero – djd97 Jul 11 '16 at 12:45
  • For some reason $_POST['pickupDateOther'] is empty even when I put a date into the field ; @asiviero – djd97 Jul 11 '16 at 12:55
0
date('l F\, d', strtotime(' +1 day'));
JDro04
  • 650
  • 5
  • 15
0

The DateTime class accepts many types of date inputs like:

$string = "today" or "tomorrow" or "1 day" or "10 years"
$date = new DateTime($string);

Then you can format the date however you want:

$formattedDate = $date->format('l, F jS');

That will format the date as: Friday, July 8th

See http://php.net/manual/en/function.date.php for a full list of possible formats.

Community
  • 1
  • 1
Ben M.
  • 311
  • 1
  • 9