Categories
PHP

Converting and Parsing a String into Date & Time

How to guess the date and time from a string with the help of strtotime() function and change the format according to your needs. You’ll also use the date_parse() and date_parse_from_format() functions which work similarly to the strtotime() function and return an associative array of date and time information.

  1. Guess date from a string and change its format
  2. date_parse() – parse string as date
  3. date_parse_from_format() – parse string as date based on given format

Example: Guess date from a string and change its format

<?php
 $date = 'august 9, 89';
 $stmp = strtotime ($date);
 $newFormat = 'd-M-Y';
 echo date($newFormat, $stmp);
 # Prints: 09-Aug-1989

For more information visit: Generate timestamp with strtotime() and Formate Date and time with date().

Example: Guess dates with strtotime() function

<?php
 $format = 'd M Y';
 echo 'Today: '. date($format).'<br>';
 # Prints: Today: 09 Aug 2022
 
 $yesterday = strtotime('Yesterday');
 echo 'Yesterday: '. date($format, $yesterday) . '<br>';
 # Prints: Yesterday: 08 Aug 2022
 
 $tommorow = strtotime('Tomorrow');
 echo 'Tomorrow: '. date($format, $tommorow). '<br>';
 # Prints: Tomorrow: 10 Aug 2022

 $lmonth = strtotime('Last Month');
 echo 'Last month: '. date($format, $lmonth).'<br>';
 # Prints: Last month: 09 Jul 2022

 $nmonth = strtotime('Next Month');
 echo 'Next month: '. date($format, $nmonth);
 # Prints: Last month: 09 Sep 2022

The whole magic is put into the PHP function strtotime(), it parses about any English textual date/time description into a UNIX timestamp. The date() function reads the given timestamp and outputs a formatted date according to the provided format keywords.

date_parse()

<?php
 //Syntax
 date_parse(string $datetime): array

Unlike the strtotime() function which converts a “date/time” string into timestamp, the date_parse() function converts a “date/time” string into an array describing that time and date. It returns false if it failed to parse the string.

<?php
 $str = 'august 9, 89';
 $date = date_parse($str);
 var_dump($date);
 /* Prints: 
 array(12) {
  ["year"]=>  int(1989)
  ["month"]=> int(8)
  ["day"]=>   int(9)
  ["hour"]=>  bool(false)
  ["minute"]=>bool(false)
  ["second"]=>bool(false)
  ["fraction"]=>   bool(false)
  ["warning_count"]=> int(0)
  ["warnings"]=> array(0) { }
  ["error_count"]=>  int(0)
  ["errors"]=>  array(0) { }
  ["is_localtime"]=> bool(false)
 } */

date_parse_from_format()

<?php
 //Syntax
 date_parse_from_format(string $format, string $datetime): array

This function takes two parameters:

  1. $format: date/time formatting characters.
  2. $datetime: a string representing the date and time.

This function parses a date/time string into an associative array representing a date/time. The string date/time is given in the format specified by $format parameter, using the same character codes as described in date() tutorial, see Date formattng symbols.

<?php
 $str = '2-8-22 5:01PM +02:00';
 $arr = date_parse_from_format('d-m-y h:iA P', $str);
 var_dump($arr);
 /* Prints: 
 array(15) {
  ["year"] => int(2022)
  ["month"]=> int(8)
  ["day"]  => int(2)
  ["hour"] => int(17)
  ["minute"]=> int(1)
  ["second"]=> int(0)
  ["fraction"] => float(0)
  ["warning_count"]=> int(0)
  ["warnings"]=> array(0) { }
  ["error_count"]=>  int(0)
  ["errors"]=>  array(0) { }
  ["is_localtime"]=>  bool(true)
  ["zone_type"]=>  int(1)
  ["zone"]     =>  int(7200)
  ["is_dst"]   =>  bool(false)
 } */


The Date and Time Tutorials: