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

时间:2013-07-11 05:25:03

标签: php date datetime timestamp mktime

我有一个日期'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 个答案:

答案 0 :(得分:6)

它会产生错误,因为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");

由于

答案 1 :(得分: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 :(得分:1)

尝试使用strtotime

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

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

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

答案 3 :(得分: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;

答案 4 :(得分: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 -------- 2018/9/23 10:05:55 PM

     

昨天没有:1537660801

     

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

     

明天:1537729199

     

昨天可读:09/23/2018 11:59:59 PM -------- 2018/09/23 10:05:55 PM

     

明天无:1537747199

     

昨天可读:09/24/2018 04:59:59 AM -------- 2018/09/23 10:05:55 PM