用 PHP 减去 1 天

发布于 2024-12-08 02:12:19 字数 634 浏览 4 评论 0原文

我正在尝试获取来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

淡淡绿茶香 2024-12-15 02:12:19

您可以尝试:

print('Next Date ' . date('Y-m-d', strtotime('-1 day', strtotime($date_raw))));

You can try:

print('Next Date ' . date('Y-m-d', strtotime('-1 day', strtotime($date_raw))));
银河中√捞星星 2024-12-15 02:12:19
$date = new DateTime("2017-05-18"); // For today/now, don't pass an arg.
$date->modify("-1 day");
echo $date->format("Y-m-d H:i:s");

使用 DateTime 显着减少了操作日期时所遭受的头痛。

$date = new DateTime("2017-05-18"); // For today/now, don't pass an arg.
$date->modify("-1 day");
echo $date->format("Y-m-d H:i:s");

Using DateTime has significantly reduced the amount of headaches endured whilst manipulating dates.

梦里°也失望 2024-12-15 02:12:19
 date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $date) ) ));
 date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $date) ) ));
天生の放荡 2024-12-15 02:12:19

单行选项是:

echo date_create('2011-04-24')->modify('-1 days')->format('Y-m-d');

在线 PHP 编辑器上运行它。


mktime 替代方案

如果您不想使用字符串方法或进行计算,或者即使创建其他变量,mktime也支持减法和负数按以下方式取值:

// Today's date
echo date('Y-m-d'); // 2016-03-22

// Yesterday's date
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); // 2016-03-21

// 42 days ago
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-42, date("Y"))); // 2016-02-09

//Using a previous date object
$date_object = new DateTime('2011-04-24');
echo date('Y-m-d',
  mktime(0, 0, 0,
     $date_object->format("m"),
     $date_object->format("d")-1,
     $date_object->format("Y")
    )
); // 2011-04-23

在线 PHP 编辑器

A one-liner option is:

echo date_create('2011-04-24')->modify('-1 days')->format('Y-m-d');

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:

// Today's date
echo date('Y-m-d'); // 2016-03-22

// Yesterday's date
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); // 2016-03-21

// 42 days ago
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-42, date("Y"))); // 2016-02-09

//Using a previous date object
$date_object = new DateTime('2011-04-24');
echo date('Y-m-d',
  mktime(0, 0, 0,
     $date_object->format("m"),
     $date_object->format("d")-1,
     $date_object->format("Y")
    )
); // 2011-04-23

Online PHP Editor

谜泪 2024-12-15 02:12:19

面向对象版本

$dateObject = new DateTime( $date_raw );
print('Next Date ' . $dateObject->sub( new DateInterval('P1D') )->format('Y-m-d');

Object oriented version

$dateObject = new DateTime( $date_raw );
print('Next Date ' . $dateObject->sub( new DateInterval('P1D') )->format('Y-m-d');
梦太阳 2024-12-15 02:12:19

不确定为什么您当前的代码不起作用,但如果您没有特别需要日期对象,那么这将起作用:

$first_date = strtotime($date_raw);
$second_date = strtotime('-1 day', $first_date);

print 'First Date ' . date('Y-m-d', $first_date);
print 'Next Date ' . date('Y-m-d', $second_date);

Not sure why your current code isn't working but if you don't specifically need a date object this will work:

$first_date = strtotime($date_raw);
$second_date = strtotime('-1 day', $first_date);

print 'First Date ' . date('Y-m-d', $first_date);
print 'Next Date ' . date('Y-m-d', $second_date);
二智少女 2024-12-15 02:12:19

Answear 取自 Php 手册 strtotime 函数注释:

echo date( "Y-m-d", strtotime( "2009-01-31 -1 day"));

或者

$date = "2009-01-31";
echo date( "Y-m-d", strtotime( $date . "-1 day"));

Answear taken from Php manual strtotime function comments :

echo date( "Y-m-d", strtotime( "2009-01-31 -1 day"));

Or

$date = "2009-01-31";
echo date( "Y-m-d", strtotime( $date . "-1 day"));
冷清清 2024-12-15 02:12:19

您可以在 date() 中添加 strtotime(),参数为周或月的天数。
日示例:

date("Y-m-d", strtotime("-1 day"));

周:

date("Y-m-d", strtotime("-1 week"));

月:

date("Y-m-d", strtotime("-1 months"));

You can add strtotime() in date() with parameter number of day week or month.
Example for day:

date("Y-m-d", strtotime("-1 day"));

For week:

date("Y-m-d", strtotime("-1 week"));

For month:

date("Y-m-d", strtotime("-1 months"));
花心好男孩 2024-12-15 02:12:19

怎么样:首先将其转换为 unix 时间戳,减去 606024(正好是一天,以秒为单位),然后从中获取日期。

$newDate = strtotime($date_raw) - 60*60*24;
echo date('Y-m-d',$newDate);

正如 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.

$newDate = strtotime($date_raw) - 60*60*24;
echo date('Y-m-d',$newDate);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文