用 PHP 减去 1 天
我正在尝试获取来自 Drupal CMS 的日期对象,减去一天并打印出两个日期。这就是我所拥有的
$date_raw = $messagenode->field_message_date[0]['value'];
print($date_raw);
//this gives me the following string: 2011-04-24T00:00:00
$date_object = date_create($date_raw);
$next_date_object = date_modify($date_object,'-1 day');
print('First Date ' . date_format($date_object,'Y-m-d'));
//this gives me the correctly formatted string '2011-04-24'
print('Next Date ' . date_format($next_date_object,'Y-m-d'));
//this gives me nothing. The output here is always blank
所以我不明白为什么原始日期对象输出得很好,但后来我尝试创建一个额外的日期对象并通过减去一天来修改它,但似乎我不能这样做。输出总是空白。
I'm trying to take a date object that's coming out of my Drupal CMS, subtract one day and print out both dates. Here's what I have
$date_raw = $messagenode->field_message_date[0]['value'];
print($date_raw);
//this gives me the following string: 2011-04-24T00:00:00
$date_object = date_create($date_raw);
$next_date_object = date_modify($date_object,'-1 day');
print('First Date ' . date_format($date_object,'Y-m-d'));
//this gives me the correctly formatted string '2011-04-24'
print('Next Date ' . date_format($next_date_object,'Y-m-d'));
//this gives me nothing. The output here is always blank
So I'm not understanding why the original date object is coming out fine, but then I'm trying to create an additional date object and modify it by subtracting one day and it seems like I can't do that. The output always comes out blank.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以尝试:
You can try:
使用 DateTime 显着减少了操作日期时所遭受的头痛。
Using DateTime has significantly reduced the amount of headaches endured whilst manipulating dates.
单行选项是:
在在线 PHP 编辑器上运行它。
mktime 替代方案
如果您不想使用字符串方法或进行计算,或者即使创建其他变量,mktime也支持减法和负数按以下方式取值:
在线 PHP 编辑器
A one-liner option is:
Running it on Online PHP Editor.
mktime alternative
If you prefer to avoid using string methods, or going into calculations, or even creating additional variables, mktime supports subtraction and negative values in the following way:
Online PHP Editor
面向对象版本
Object oriented version
不确定为什么您当前的代码不起作用,但如果您没有特别需要日期对象,那么这将起作用:
Not sure why your current code isn't working but if you don't specifically need a date object this will work:
Answear 取自 Php 手册 strtotime 函数注释:
或者
Answear taken from Php manual strtotime function comments :
Or
您可以在
date()
中添加strtotime()
,参数为周或月的天数。日示例:
周:
月:
You can add
strtotime()
indate()
with parameter number of day week or month.Example for day:
For week:
For month:
怎么样:首先将其转换为 unix 时间戳,减去 606024(正好是一天,以秒为单位),然后从中获取日期。
正如 apokryfos 指出的那样:请注意,这会减去实际的一天,但如果您所在的地方实行夏令时,那么如果您需要一天中的同一个小时,可能会导致问题。但是,您不应该在您的服务器上使用除 UTC 之外的任何东西!从技术上讲,闰秒也可能存在问题
How about this: convert it to a unix timestamp first, subtract 606024 (exactly one day in seconds), and then grab the date from that.
As apokryfos pointed out: Note, this subtracts an actual day, but if you are in a place with Daylight Savings time, it could cause a problem if you need the same hour of day. However, YOU SHOULDN'T BE USING ANYTHING BUT UTC ON YOUR SERVER! Also technically there could be an issue with leap seconds