php 如何在php中将日期和时间转换为时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17585632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert date and time to timestamp in php?
提问by Patrioticcow
i have a date '07/23/2009'
and a time '18:11'
and i want to get a timestamp out of it :
here is my example:
我有一个日期'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");
the issue is that $x
gives me the current timestamp.
问题是$x
给了我当前的时间戳。
any ideas?
有任何想法吗?
回答by divyang asodiya
it gives error because mktime function require all values of numbers only and this function gives only date . if you try like
它给出了错误,因为 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));
then it will work.
然后它会起作用。
so your complete code will be
所以你的完整代码将是
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>";
//if you want timestamp then use
//如果你想要时间戳然后使用
echo strtotime("07/23/2009 18:11");
Thanks
谢谢
回答by vascowhite
Use the DateTimeclass:-
使用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();
or:-
或者:-
$dateStr = '07/23/2009 18:11';
$timestamp = \DateTime::createFromFormat('m/d/Y H:i', $dateStr)->getTimestamp();
回答by Orangepill
Try using strtotime
尝试使用 strtotime
$x = strtotime($date." ".$time);
for your case your code should be
对于您的情况,您的代码应该是
date_default_timezone_set('UTC');
$x = strtotime("07/23/2009 18:11");
echo $x;
回答by Sundar
<?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;
回答by MR_AMDEV
IMPORTANT:
重要的:
In addition to above answers ,there is an important thing that one must follow.Always use the With() function(see below) i.e
除了上述答案之外,还有一件重要的事情必须遵循。始终使用 With() 函数(见下文)即
Always use :
始终使用:
$newTimezone = new DateTime($day);
$newTimezone->setTimezone(new DateTimeZone($timezone));
Do not use:
不使用:
$newTimezone = new DateTime($day, new DateTimeZone($timezone));
REASON:(different outputs, check below)
原因:(不同的输出,检查下面)
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>';
OUTPUTS:
输出:
With Yesterday: 1537642801
With Yesterday Readable: 09/23/2018 12:00:01 AM -------- 09/23/2018 10:05:55 PM
Without Yesterday: 1537660801
With Yesterday Readable: 09/23/2018 05:00:01 AM -------- 09/23/2018 10:05:55 PM
With Tomorrow: 1537729199
With Yesterday Readable: 09/23/2018 11:59:59 PM -------- 09/23/2018 10:05:55 PM
Without Tomorrow: 1537747199
With Yesterday Readable: 09/24/2018 04:59:59 AM -------- 09/23/2018 10:05:55 PM
与昨天: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