I tried
我试着
$dtToday = DateTime::createFromFormat('Y-m-d', date('Y-m-d'));
but when I output it
但当我输出它。
die($dtToday->format('d M Y g:i:s a'));
I still get the time eg "22 Jan 2011 4:53:59 pm". Why is that?
我还能得到时间,比如“2011年1月22日下午4点53分59秒”。这是为什么呢?
UPDATE
更新
Ah... many people misunderstood me, my bad, I forgot to point out the main point. I created the date with just the date portion, I don't want the time. So I'd expect something like
啊…许多人误解了我,我的坏,我忘了指出要点。我用日期部分创建了日期,我不需要时间。所以我希望有类似的东西。
22 Jan 2011 12:00:00 am
7 个解决方案
#1
13
OK, I admit that I am not familiar with the DateTime class. But I am sure it provides methods such as setHour, setMinute etc which you can use to zero out the time portion. Personally, I'd create the date with the mktime()
function:
好的,我承认我不熟悉DateTime类。但是我确信它提供了一些方法,比如setHour, setMinute等,你可以用它来排除时间部分。就个人而言,我将使用mktime()函数创建日期:
echo date( "Y-m-d H:i:s", // this line is for demonstration
mktime(0, 0, 0) // this generates a timestamp which can be used in most date functions
// first three parameters are hour, minute, second which I set to 0
// remaining parameters are optional
// infact all parameters are optional and all parameters default to corresponding portion of the *current* date/time
);
#2
12
See the documentation for DateTime::createFromFormat
:
查看DateTime的文档::createFromFormat:
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
如果格式不包含字符!然后,将在当前系统时间内设置未指定格式的生成时间的部分。
If you do the following function call, you'll get the result you expect:
如果您执行以下函数调用,您将得到您期望的结果:
$dtToday = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
#3
5
You can do this by passing the current unix timestamp as the second parameter to the date function
您可以通过将当前的unix时间戳作为第二个参数传递到日期函数来实现这一点。
echo date("Y-m-d H:i:s",time());
#4
5
Today's start timestamp
今天的开始时间戳
$today_start_ts = strtotime(date('Y-m-d', time()). '00:00:00');
#5
1
You are getting "22 Jan 2011 4:53:59 pm" because those are the rules you format your date with :
d (day) : 22
M (Month) : Jan
Y (Year) : 2011
g (12-hour format) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
Be more speciffic about the format would you like your timestamp to have. I suggest you take a peak at the php date documentation.
你越来越“2011年1月22日4:53:59点”,因为这些规则是你格式日期:d(天):22米(月):1月Y(年):2011克(12小时格式):我(分钟):53年代(秒):59(am / pm):点更speciffic关于您的时间戳的格式。我建议您在php日期文档中取一个峰值。
#6
0
Is it using UTC, or something?
是用UTC,还是别的什么?
I have a PHP version that gives me an error whenever I do something date
related without first using date_default_timezone_set
. Maybe that'll help you.
我有一个PHP版本,每当我在不使用date_default_timezone_set的情况下做一些与日期相关的事情时,都会给我一个错误。也许这能对你有所帮助。
#7
0
Remove this part g:i:s a
from your code.
删除这部分g:i:s a从你的代码。
Now, if you want a nice date formatted according to your local, i recommand you to use strftime()
function.
现在,如果您想要根据您的本地格式进行格式化,我建议您使用strftime()函数。
#1
13
OK, I admit that I am not familiar with the DateTime class. But I am sure it provides methods such as setHour, setMinute etc which you can use to zero out the time portion. Personally, I'd create the date with the mktime()
function:
好的,我承认我不熟悉DateTime类。但是我确信它提供了一些方法,比如setHour, setMinute等,你可以用它来排除时间部分。就个人而言,我将使用mktime()函数创建日期:
echo date( "Y-m-d H:i:s", // this line is for demonstration
mktime(0, 0, 0) // this generates a timestamp which can be used in most date functions
// first three parameters are hour, minute, second which I set to 0
// remaining parameters are optional
// infact all parameters are optional and all parameters default to corresponding portion of the *current* date/time
);
#2
12
See the documentation for DateTime::createFromFormat
:
查看DateTime的文档::createFromFormat:
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
如果格式不包含字符!然后,将在当前系统时间内设置未指定格式的生成时间的部分。
If you do the following function call, you'll get the result you expect:
如果您执行以下函数调用,您将得到您期望的结果:
$dtToday = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
#3
5
You can do this by passing the current unix timestamp as the second parameter to the date function
您可以通过将当前的unix时间戳作为第二个参数传递到日期函数来实现这一点。
echo date("Y-m-d H:i:s",time());
#4
5
Today's start timestamp
今天的开始时间戳
$today_start_ts = strtotime(date('Y-m-d', time()). '00:00:00');
#5
1
You are getting "22 Jan 2011 4:53:59 pm" because those are the rules you format your date with :
d (day) : 22
M (Month) : Jan
Y (Year) : 2011
g (12-hour format) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
Be more speciffic about the format would you like your timestamp to have. I suggest you take a peak at the php date documentation.
你越来越“2011年1月22日4:53:59点”,因为这些规则是你格式日期:d(天):22米(月):1月Y(年):2011克(12小时格式):我(分钟):53年代(秒):59(am / pm):点更speciffic关于您的时间戳的格式。我建议您在php日期文档中取一个峰值。
#6
0
Is it using UTC, or something?
是用UTC,还是别的什么?
I have a PHP version that gives me an error whenever I do something date
related without first using date_default_timezone_set
. Maybe that'll help you.
我有一个PHP版本,每当我在不使用date_default_timezone_set的情况下做一些与日期相关的事情时,都会给我一个错误。也许这能对你有所帮助。
#7
0
Remove this part g:i:s a
from your code.
删除这部分g:i:s a从你的代码。
Now, if you want a nice date formatted according to your local, i recommand you to use strftime()
function.
现在,如果您想要根据您的本地格式进行格式化,我建议您使用strftime()函数。