Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date_parse_from_format to unix timestamp

Tags:

php

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?

like image 353
Johan Avatar asked Dec 12 '22 20:12

Johan


1 Answers

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)
like image 159
phpisuber01 Avatar answered Dec 23 '22 09:12

phpisuber01