Home > Articles

Date and Time

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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.

 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().

  • + Share This
  • 🔖 Save To Your Account