Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php减去以下类型中的日期_Php_Date_Subtraction - Fatal编程技术网

如何使用php减去以下类型中的日期

如何使用php减去以下类型中的日期,php,date,subtraction,Php,Date,Subtraction,假设我有一个这种格式的日期 YearMonthDayHourMinuteSecond 比如说 20130529051043 我需要从中减去1秒,直到它达到-3小时 所以,直到它成为 20130529021043 如何使用PHP实现这一点 $current = \DateTime::createFromFormat("YmdHis", "20130529051043"); $end = \DateTime::createFromFormat("YmdHis", "20130529051043

假设我有一个这种格式的日期

YearMonthDayHourMinuteSecond
比如说

20130529051043
我需要从中减去1秒,直到它达到-3小时 所以,直到它成为

20130529021043
如何使用PHP实现这一点

$current = \DateTime::createFromFormat("YmdHis", "20130529051043");
$end = \DateTime::createFromFormat("YmdHis", "20130529051043")->modify("-3 hours");

while ($current > $end) {
    $current = $current->modify("-1 second");
    // do your stuff
}
程序版本:

$current = date_create_from_format("YmdHis", "20130529051043");
$end = date_modify(date_create_from_format("YmdHis", "20130529051043"), "-3 hours");

while ($current > $end) {
    $current = date_modify($current, "-1 second");
    // do your stuff
}
程序版本:

$current = date_create_from_format("YmdHis", "20130529051043");
$end = date_modify(date_create_from_format("YmdHis", "20130529051043"), "-3 hours");

while ($current > $end) {
    $current = date_modify($current, "-1 second");
    // do your stuff
}

你可以用strotime

$timestamp = strtotime("20130529051043");
如果你在三小时前的时间之后,你可以减去(60*60*3)

要显示,请使用
日期
功能

echo date("Y-m-d H:i:s", $three_hours_ago), " is three hours before ", date("Y-m-d H:i:s", $timestamp);

你可以用strotime

$timestamp = strtotime("20130529051043");
如果你在三小时前的时间之后,你可以减去(60*60*3)

要显示,请使用
日期
功能

echo date("Y-m-d H:i:s", $three_hours_ago), " is three hours before ", date("Y-m-d H:i:s", $timestamp);

您可以将类与方法一起使用

您可以将类与方法一起使用

用DateTime::CreateFromFormat解析它用DateTime::CreateFromFormat解析它您可以将其修改为过程性而不是OOP吗?PHP可捕获致命错误:类DateTime的对象无法转换为Strings在哪一行中获得此结果?两者都有变量$current和$end
$current=date_从_格式(“YmdHis”,“20130529051043”)中创建_的行
非常适合我。你能将其修改为过程性而不是OOP吗?PHP可捕获的致命错误:类DateTime的对象无法转换为stringIn在哪一行中获得此信息?变量$current和$end
$current=date_create_from_format(“YmdHis”,“20130529051043”)中的两行非常适合我。