开发者

PHP parsing a date using strptime() or strtotime()

开发者 https://www.devze.com 2022-12-18 14:31 出处:网络
HI, I have a problem parsing a date in php using strptime() as my system is running w开发者_JS百科indows. I\'ve also try to parse the date using strtotime() but not luck either because I\'m using euro

HI, I have a problem parsing a date in php using strptime() as my system is running w开发者_JS百科indows. I've also try to parse the date using strtotime() but not luck either because I'm using euro date format.

This is the date format I'm using 30/11/2009 11:00 am and I need to get the timestamp from it.

Any ideas how can I achieve this ??


$us = preg_replace('#^(\d+)/(\d+)/(\d+)\s([\d:]+)\s([a-zA-z]{0,2})$#','$3-$2-$1 $4 $5', $date);
$timestamp = strtotime($date);


If your consistently using that date, just break up the date into multiple date pieces using substring.

$dayofmonth = substr($date,1,2)
$month = substr($date,4,2);
$year = substr($date,7,4);

etc, etc.


dont use strtotime for this format... from the php doc:

The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now , or the current time if now is not supplied.

use one of the supplied methods in the other answers.


You can use this function:

         function convert_datetime($str)
            {

            list($date, $time) = explode(' ', $str);
            list($day, $month, $year) = explode('/', $date);
            list($hour, $minute) = explode(':', $time);

            $timestamp = mktime($hour, $minute, 00, $month, $day, $year);

            return $timestamp;
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号