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给了我当前的时间戳。

有任何想法吗?

4

5 回答 5

6

它给出了错误,因为 mktime 函数只需要所有数字值,而这个函数只给出 date 。如果你尝试喜欢

$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");

谢谢

于 2013-07-11T05:58:58.690 回答
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();
于 2013-07-11T17:45:09.813 回答
2

尝试使用 strtotime

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

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

date_default_timezone_set('UTC');
$x = strtotime("07/23/2009 18:11");
echo $x;
于 2013-07-11T05:26:34.673 回答
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;
于 2013-07-11T05:51:09.500 回答
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

于 2018-09-23T17:21:19.200 回答