How to convert date to timestamp in PHP?

2018-12-31 07:45发布

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

19条回答
情到深处是孤独
2楼-- · 2018-12-31 07:50

Please be careful about time/zone if you set it to save dates in database, as I got an issue when I compared dates from mysql that converted to timestamp using strtotime. you must use exactly same time/zone before converting date to timestamp otherwise, strtotime() will use default server timezone.

Please see this example: https://3v4l.org/BRlmV

function getthistime($type, $modify = null) {
    $now = new DateTime(null, new DateTimeZone('Asia/Baghdad'));
    if($modify) {
        $now->modify($modify);
    }
    if(!isset($type) || $type == 'datetime') {
        return $now->format('Y-m-d H:i:s');
    }
    if($type == 'time') {
        return $now->format('H:i:s');
    }
    if($type == 'timestamp') {
        return $now->getTimestamp();
    }
}
function timestampfromdate($date) {
    return DateTime::createFromFormat('Y-m-d H:i:s', $date, new DateTimeZone('Asia/Baghdad'))->getTimestamp();
}

echo getthistime('timestamp')."--".
    timestampfromdate(getthistime('datetime'))."--".
    strtotime(getthistime('datetime'));

//getthistime('timestamp') == timestampfromdate(getthistime('datetime')) (true)
//getthistime('timestamp') == strtotime(getthistime('datetime')) (false)
查看更多
长期被迫恋爱
3楼-- · 2018-12-31 07:50

Use PHP function date()

echo date('m/d/Y', 1299446702);

date — Format a local time/date

查看更多
浪荡孟婆
4楼-- · 2018-12-31 07:53

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.

查看更多
明月照影归
5楼-- · 2018-12-31 07:55


If you have PHP 5.3 or above,

this method works on both Windows and Unix and is time-zone aware, which is probably what you want if you are serious about working with dates.

If you don't care about timezone, or want to use the time zone your server uses:

$d = DateTime::createFromFormat('d-m-Y', '22-09-2008');
echo $d->getTimestamp();

1222093324 (This will differ depending on your server time zone...)


If you want to specify in which time zone, here EST. (Same as New York.)

$d = DateTime::createFromFormat('d-m-Y', '22-09-2008', new DateTimeZone('EST'));
echo $d->getTimestamp();

1222093305


Or if you want to use UTC. (Same as "GMT".)

$d = DateTime::createFromFormat('d-m-Y', '22-09-2008', new DateTimeZone('UTC'));
echo $d->getTimestamp();

1222093289


查看更多
看风景的人
6楼-- · 2018-12-31 08:01

If you know the format use strptime because strtotime does a guess for the format, which might not always be correct. Since strptime is not implemented in Windows there is a custom function

Remember that the returnvalue tm_year is from 1900! and tm_month is 0-11

Example:

$a = strptime('22-09-2008', '%d-%m-%Y');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900)
查看更多
十年一品温如言
7楼-- · 2018-12-31 08:02

Given that the function strptime() does not work for Windows and strtotime() can return unexpected results, I recommend using date_parse_from_format():

$date = date_parse_from_format('d-m-Y', '22-09-2008');
$timestamp = mktime(0, 0, 0, $date['month'], $date['day'], $date['year']);
查看更多
登录 后发表回答