How to convert date to timestamp in PHP?

前端 未结 19 1851
暗喜
暗喜 2020-11-22 05:38

How do I get timestamp from e.g. 22-09-2008?

19条回答
  •  抹茶落季
    2020-11-22 06:37

    Here is how I'd do it:

    function dateToTimestamp($date, $format, $timezone='Europe/Belgrade')
    {
        //returns an array containing day start and day end timestamps
        $old_timezone=date_timezone_get();
        date_default_timezone_set($timezone);
        $date=strptime($date,$format);
        $day_start=mktime(0,0,0,++$date['tm_mon'],++$date['tm_mday'],($date['tm_year']+1900));
        $day_end=$day_start+(60*60*24);
        date_default_timezone_set($old_timezone);
        return array('day_start'=>$day_start, 'day_end'=>$day_end);
    }
    
    $timestamps=dateToTimestamp('15.02.1991.', '%d.%m.%Y.', 'Europe/London');
    $day_start=$timestamps['day_start'];
    

    This way, you let the function know what date format you are using and even specify the timezone.

提交回复
热议问题