- Using Text Within date()
- Automatically Localizing Dates
- Manually Localizing Dates
- Using the Current Date the U.S./U.K./European Way
- Formatting a Specific Date
- Validating a Date
- Calculating a Relative Date
- Creating a Sortable Time Stamp
- Converting a String into a Date
- Determining Sunrise and Sunset
- Using Date and Time for Benchmarks
- Using Form Fields for Date Selection
- Create Self-updating Form Fields for Date Selection
- Calculating the Difference Between Two Dates
- Using GMT Date/Time Information
Converting a String into a Date
‘Yesterday: ‘ . date(‘r‘, strtotime(‘Yesterday‘))
Previously, you saw a numeric representation of a date—either a triplet of day, month, and year, or a time stamp value. This time, you can go the other way and convert a string representation of a date/time value into an epoche value or something else that is usable within PHP and its date/time functions.
<?php echo ‘Yesterday: ‘ . date(‘r‘, strtotime(‘Yesterday‘)) . ‘<br />‘; echo ‘Today: ‘ . date(‘r‘, strtotime(‘Today‘)) . ‘<br />‘; echo ‘Tomorrow: ‘ . date(‘r‘, strtotime(‘Tomorrow‘)) . ‘<br />‘; echo ‘In one week: ‘ . date(‘r‘, strtotime(‘+1 week‘)) . ‘<br />‘; echo ‘One month before: ‘ . date(‘r‘, strtotime(‘- 1 month‘)) . ‘<br />‘; echo ‘Last Sunday: ‘ . date(‘r‘, strtotime(‘Last Sunday‘)) . ‘<br />‘; echo ‘Next fantasy day: ‘ . var_export(@date(‘r‘, strtotime(‘Next fantasy day‘)), true); ?>
Converting a String into a Date (strtotime.php)
The whole magic is put into the PHP function strtotime(). According to the documentation, it "parse[s] about any English textual date/time description into a UNIX time stamp." It sounds amazing, and it is amazing. The basis for this is the GNU date syntax; the code at the beginning of this phrase shows some examples for strtotime().