PHP

Converting a String into a Date

'Yesterday: ' . date('r', strtotime('Yesterday'))

Previously, you saw a numeric representation of a dateeither 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.

Example: Converting a String into a Date

<?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);
?>

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 shows some examples for strtotime().

At the time of this writing, strtotime() shows some strange behavior when a relative date is calculated and a change from or to DST is imminent. Also at the time of this writing, PHP's date/time functions are about be rewritten and amended.

by BrainBellupdated
Advertisement: