Skip to content
Advertisement

date_parse_from_format to unix timestamp

I have a question regarding the date_parse_from_format() method:

$parsed = date_parse_from_format("Y d M H:i T", '2012 28 Nov 21:00 CET');

Returns associative array with detailed info about given date.

This will return an array with some date info. But is there any way to convert it to a unix timestamp?

Advertisement

Answer

Johan,

Heres an exact example of how to implement mktime into your scenario:

$parsed = date_parse_from_format("Y d M H:i T", '2012 28 Nov 21:00 CET');
$new = mktime(
        $parsed['hour'], 
        $parsed['minute'], 
        $parsed['second'], 
        $parsed['month'], 
        $parsed['day'], 
        $parsed['year']
);
echo $new; //returns: 1354136400

For future reference, you can use var_dump() or print_r() on an array to see how to access the nested values.

var_dump of $parsed

array (size=16)
  'year' => int 2012
  'month' => int 11
  'day' => int 28
  'hour' => int 21
  'minute' => int 0
  'second' => int 0
  'fraction' => boolean false
  'warning_count' => int 0
  'warnings' => 
    array (size=0)
      empty
  'error_count' => int 0
  'errors' => 
    array (size=0)
      empty
  'is_localtime' => boolean true
  'zone_type' => int 2
  'zone' => int -60
  'is_dst' => boolean false
  'tz_abbr' => string 'CET' (length=3)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement