php 如何减去分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17717911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Subtract Minutes
提问by user2536185
I want to send a reminder email.I don't want to use cron
on Linux/Unix/BSD box or Scheduled Tasks on Windows.
我想发送一封提醒邮件。我不想cron
在 Linux/Unix/BSD 机器上使用或在 Windows 上使用计划任务。
I'm trying to subtract 15 minutes from the current time.
我正在尝试从当前时间中减去 15 分钟。
here is my code so far (doesn't work):
到目前为止,这是我的代码(不起作用):
$days = date("j",time());
$months = date("n",time());
$years = date("Y",time());
$hours = date("G",time());
$mins = (date("i",time()));
$secs = date("s",time());
$mins = $mins-15;
回答by MrCode
To subtract 15 minutes from the current time, you can use strtotime()
:
要从当前时间减去 15 分钟,您可以使用strtotime()
:
$newTime = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $newTime);
回答by Thomas Clayson
Change the date into a timestamp (in seconds) then minus 15 minutes (in seconds) and then convert back to a date:
将日期更改为时间戳(以秒为单位),然后减去 15 分钟(以秒为单位),然后再转换回日期:
$date = date("Y-m-d H:i:s");
$time = strtotime($date);
$time = $time - (15 * 60);
$date = date("Y-m-d H:i:s", $time);
回答by DevZer0
You can use DateInterval
您可以使用 DateInterval
$date = new DateTime();
$interval = new DateInterval("PT15M");
$interval->invert = 1;
$date->add($interval);
echo $date->format("c") . "\n";
回答by Adil Abbasi
you can try this as well,
你也可以试试这个
$dateTimeMinutesAgo = new DateTime("15 minutes ago");
$dateTimeMinutesAgo = $dateTimeMinutesAgo->format("Y-m-d H:i:s");
回答by eX0du5
How about substracting the 15 minutes from time() before converting it?
在转换之前从 time() 中减去 15 分钟怎么样?
$time = time() - (15 * 60);
And then use $time instead of time() in your code.
然后在代码中使用 $time 而不是 time() 。
回答by zkanoca
$currentTime = date('Y-m-d H:i:s');
$before15mins = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $before15mins);
回答by Kshitiz
Following is the way you can add days / hours / minutes / sec to current time
以下是您可以将天/小时/分钟/秒添加到当前时间的方法
$addInterval = date('Y-m-d H:i:s', strtotime("+$days days $hours hours $minute minute $sec second", strtotime(currentTime)));
回答by Paul Wright
Try using
尝试使用
$min = time() - 900; //900 seconds = 15 minutes
回答by Sintu Roy
You can also use DateInterval object
您还可以使用 DateInterval 对象
<?php
$date = new DateTime('Y-m-d H:i:s');
$date->sub(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s');?>
回答by Pascut
To subtract 15 minutes you can do:
要减去 15 分钟,您可以执行以下操作:
date('Y-m-d H:i:s', (time() - 60 * 15));
You can replace 15 with the number of minutes you want.
您可以将 15 替换为所需的分钟数。
In case you're looking to subtract seconds you can simply do:
如果您想减去秒数,您可以简单地执行以下操作:
date('Y-m-d H:i:s', (time() - 10));
In this way you'll subtract 10 seconds.
通过这种方式,您将减去 10 秒。