php 将单独的月、日和年值转换为时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6208484/
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
Converting separate month, day and year values into a timestamp
提问by A Clockwork Orange
I have a month value (1-12), day value (1-31), and a year value (2010,2011,2012). I also have a hour value and a minute value.
我有一个月值 (1-12)、日值 (1-31) 和一个年值 (2010,2011,2012)。我也有一个小时值和一个分钟值。
How can I give this to strtotime()
in a way it can convert it to a timestamp?
我怎样才能以strtotime()
一种可以将其转换为时间戳的方式提供它?
回答by Nicolás Ozimica
Given the variables $year
$month
$day
$hour
$minute
you can do:
鉴于$year
$month
$day
$hour
$minute
您可以执行的变量:
strtotime("$year-$month-$day $hour:$minute");
Be careful to enclose the variables with "
, never in this case with '
.
小心将变量用 括起来"
,在这种情况下千万不要用 括起来'
。
UPDATE (thanks to comments of @Clockwork and @Tadeck):
更新(感谢@Clockwork 和@Tadeck 的评论):
In case there is a variable $timeofday
that represents the time of day(i.e. AM or PM),
then you can parse it this with:
如果有一个变量$timeofday
表示一天中的时间(即 AM 或 PM),那么您可以使用以下方法对其进行解析:
strtotime("$year-$month-$day $hour:$minute$timeofday");
that is to say, just attach that variable to the end of the text.
也就是说,只需将该变量附加到文本的末尾即可。
回答by Madhawa Priyashantha
why convert string to date when you already know year month and date.
当您已经知道年月和日期时,为什么要将字符串转换为日期。
use setDate
funtion
使用setDate
功能
<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>
回答by Madhawa Priyashantha
回答by Tadeck
You can provide it to function strtotime()
in many ways, as mentioned in documentation. Some examples include:
您可以通过strtotime()
多种方式提供它以发挥作用,如文档中所述。一些例子包括:
$your_time = strtotime('12/31/2011 9:59');
$your_time = strtotime('2011-12-31 9:59');
$your_time = strtotime('December 31, 2011 9:59');
etc. It really is very flexible.
等等。它真的很灵活。
You can find the list of valid formats in the documentation, and that is (from the "Compound Formats" list in the mentioned documentation) for example:
您可以在文档中找到有效格式的列表,例如(来自上述文档中的“复合格式”列表):
10/Oct/2000:13:55:36 -0700
,2008:08:07 18:11:31
,2008-08-07 18:11:31
,2008-07-01T22:35:17.02
,2008-07-01T22:35:17.03+08:00
,20080701T22:38:07
,20080701T9:38:07
,20080701t223807
,20080701T093807
,2008-7-1T9:3:37
,
10/Oct/2000:13:55:36 -0700
,2008:08:07 18:11:31
,2008-08-07 18:11:31
,2008-07-01T22:35:17.02
,2008-07-01T22:35:17.03+08:00
,20080701T22:38:07
,20080701T9:38:07
,20080701t223807
,20080701T093807
,2008-7-1T9:3:37
,
(this is really copy of the documentation)
(这实际上是文档的副本)
回答by anubhava
Use it like this strtotime("YYYY-mm-DD HH:MM AM/PM")
:
像这样使用它strtotime("YYYY-mm-DD HH:MM AM/PM")
:
echo date("d F Y h:i:s A", strtotime("2011-06-01 11:15 PM")) . "\n";
OUTPUT
输出
01 June 2011 11:15:00 PM
回答by dynamic
Y-m-d hh:mm
will work
Y-m-d hh:mm
将工作
echo strtotime('2011-12-14 11:44 am');
cit @Pekka :)
引用@Pekka :)
回答by patapizza
strtotime($month."-".$day."-".$year)