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)?
date('l F\, d', strtotime(' +1 day'));
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));
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.