如何在PHP中将日期和时间转换为时间戳?

4

我有一个日期'07/23/2009'和一个时间'18:11',我想从中获取一个时间戳:

date_default_timezone_set('UTC');

$d = str_replace('/', ', ', '07/23/2009');
$t = str_replace(':', ', ', '18:11');
$date = $t.', 0, '.$d;
echo $date;
echo '<br>';
echo $x = mktime("$date");

问题是$x给我当前时间戳。
有什么想法吗?
5个回答

7

出现错误是因为mktime函数要求所有值都是数字,而该函数只提供日期。如果您尝试这样做:

$h = 18;
$i = 11;
$s = 00;
$m = 07;
$d =23;
$y = 2009;
echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y));

然后它就会工作。

所以你完整的代码将是

date_default_timezone_set('UTC');

$d = str_replace('/', ',', '07/23/2009');
$t = str_replace(':', ',', '18:11');
$date = $t.',0,'.$d;
$fulldate = explode(',',$date);
echo '<br>';
$h = $fulldate[0];
$i = $fulldate[1];
$s = $fulldate[2];
$m = $fulldate[3];
$d =$fulldate[4];
$y = $fulldate[5];

echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y)) . "<br>";

//如果您需要时间戳,则使用

echo strtotime("07/23/2009 18:11");

谢谢


3

使用DateTime类:

$dateStr = '07/23/2009';
$timeStr = '18:11';
list($hours, $minutes) = explode(':', $timeStr);

$dateTime = \DateTime::createFromFormat('m/d/Y', $dateStr)->setTime($hours, $minutes);
$timeStamp = $dateTime->getTimestamp();

或者:-

$dateStr = '07/23/2009 18:11';
$timestamp = \DateTime::createFromFormat('m/d/Y H:i', $dateStr)->getTimestamp();

2

尝试使用strtotime函数。

$x = strtotime($date." ".$time);

针对您的情况,您的代码应该是

date_default_timezone_set('UTC');
$x = strtotime("07/23/2009 18:11");
echo $x;

这可能会导致日期07/06/2009 18:11的问题 - 这是7月7日还是6月6日?原因是并不是每个人(实际上只有少数国家)都使用MM/DD/YYYY格式来表示日期,大多数使用DD/MM/YYYY。最好始终将日期存储在YYYY-MM-DD格式中。 - Tigger
1
strtotime的解释方式(至少在我的语言环境中)是,如果您使用“/”作为分隔符,则它表示月/日/年;如果您使用“.”,则表示日.月.年;如果您使用“-”,则表示年-月-日。但我同意Y-m-d更受欢迎(ISO 8601)。 - Orangepill

1
<?php 

//current time zone to UTC
$date = new DateTime($datetime, new DateTimeZone(date_default_timezone_get()));

//set the time zone as UTC
$date->setTimezone(new DateTimeZone('UTC'));

//convert local time to UTC time
$date=$date->format("Y-M-D");

echo $date;

//--------------------------------------------------------------------

//UTC to some other time zone format

$date = new DateTime($datetime, new DateTimeZone("UTC"));

//set the time zone as UTC
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));

//convert local time to UTC time
$date=$date->format("Y-M-D");

echo $date;

你的代码有一点小问题。请看下面我的回答! - MR_AMDEV

0

重要提示:

除了以上的答案,还有一件非常重要的事情必须遵循。始终使用With()函数(见下文)即可。

始终使用:

$newTimezone = new DateTime($day);
$newTimezone->setTimezone(new DateTimeZone($timezone)); 

请勿使用:

$newTimezone = new DateTime($day, new DateTimeZone($timezone));

原因:(输出不同,请查看下面)

function with($day,$timezone){
    $newTimezone = new DateTime($day);
    $newTimezone->setTimezone(new DateTimeZone($timezone)); 
    $timestamp = $newTimezone->format('U');
    return $timestamp;

}    
function without($day,$timezone){
    $newTimezone = new DateTime($day, new DateTimeZone($timezone));
    $timestamp = $newTimezone->format('U');
    return $timestamp;
}   
     
$tomorrow = date('Y-m-d h:i:s A', strtotime('-1 seconds ' ,strtotime('tomorrow midnight')));
$yesterday = date('Y-m-d h:i:s A', strtotime('+24 hours 1 seconds ' , strtotime('yesterday midnight')));
$timezone = 'UTC';
        


echo 'With Yesterday: '.with($yesterday,$timezone).'<br>';
    $now = new DateTime('@'.with($yesterday,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 

echo 'Without Yesterday: '.without($yesterday,$timezone).'<br>';
    $now = new DateTime('@'.without($yesterday,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 


echo 'With Tomorrow: '.with($tomorrow,$timezone).'<br>';
    $now = new DateTime('@'.with($tomorrow,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 


echo 'Without Tomorrow: '.without($tomorrow,$timezone).'<br>';
    $now = new DateTime('@'.without($tomorrow,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 

输出:

昨天的时间戳: 1537642801

昨天可读格式: 09/23/2018 12:00:01 AM -------- 09/23/2018 10:05:55 PM

不包含昨天的时间戳: 1537660801

不包含昨天的可读格式: 09/23/2018 05:00:01 AM -------- 09/23/2018 10:05:55 PM

明天的时间戳: 1537729199

明天可读格式: 09/23/2018 11:59:59 PM -------- 09/23/2018 10:05:55 PM

不包含明天的时间戳: 1537747199

不包含明天的可读格式: 09/24/2018 04:59:59 AM -------- 09/23/2018 10:05:55 PM


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接