从给定的时间减去一秒钟 [英] Subtract one second from a given time

查看:188
本文介绍了从给定的时间减去一秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加1天,然后从给定的时间减去(减)1秒。

I would like to add 1 day and then subtract ( minus ) 1 second from a given time.

我做了:

$fromDate = date("Y-m-d", strtotime("2012-09-28")).' 00:00:00';
$date = strtotime(date("y-m-d H:m:s", strtotime($fromDate)) . " +1 day") - 1;
$toDate = date('Y-m-d H:m:s', $date);
echo $toDate;

而不是 2012-09-28 23:59:59 它返回 2012-09-29 00:09:59

我做错了什么?

推荐答案

您将圆满完成,而不是转到代码中。这是我的解决方案与 DateTime 对象:

You're going round and round instead of getting to the point in your code. Here's my solution with DateTime objects:

$time = new DateTime("2012-09-28");
$time->modify("+1 day");
$time->modify("-1 second");

var_dump($time);

或者,如果您只需要一天的最后一秒,为什么不只是:

Or, if you just need the last second of the day, why not just:

$time = "2012-09-28";
$time .= " 23:59:59";

由于每天更改秒/分/小时的可能性不大。

As it's unlikely that the number of seconds/minutes/hours a day to change.

这篇关于从给定的时间减去一秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆