php 获取 30 天回溯日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26044844/
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
Get 30 days back date along with time
提问by Mumbai CabinCrew
I want to calculate EXACT past 30 days time period in php from now (for example 30 aug 14 23:06) to 30 days back (for example 1 aug 14 23:06). I wrote this where current datetime goes in $d1 and past 30 days datetime goes in $d2 but somehow i am not getting correct results. Any idea?
我想计算从现在(例如 8 月 14 日 30 日 23:06)到 30 天后(例如 8 月 14 日 23:06)在 php 中过去 30 天的确切时间段。我写了这篇文章,当前日期时间在 $d1 中,过去 30 天日期时间在 $d2 中,但不知何故我没有得到正确的结果。任何的想法?
$url=$row["url"];
$pageid=getPageID($url);
$date=date('y-m-d g:i');
$d1=strtotime($date);
$d2=date(strtotime('today - 30 days'));
Thanks
谢谢
回答by BenM
The problem is likely caused by the malformed date()
call. The first argument passed to date()
should be the format (as shown in the Docs) and the second should be an optional timestamp.
问题很可能是由格式错误的date()
调用引起的。传递给的第一个参数date()
应该是格式(如Docs所示),第二个参数应该是可选的时间戳。
Try this:
尝试这个:
$d2 = date('c', strtotime('-30 days'));
As a short aside, the whole snippet can be simplified as follows:
简而言之,整个代码段可以简化如下:
$url = $row["url"];
$pageid = getPageID($url);
$date = date('y-m-d g:i');
$d1 = time();
$d2 = date('y-m-d g:i', strtotime('-30 days'));
回答by hek2mgl
You can also use the DateTime
class's sub()
method together with an DateInterval
:
您还可以将DateTime
类的sub()
方法与 一起使用DateInterval
:
$now = new DateTime();
$back = $now->sub(DateInterval::createFromDateString('30 days'));
echo $back->format('y-m-d g:i');
回答by Shareful
Very simple two lines of code
$date = new DateTime();
echo $date->modify('-30 day')->format('y-m-d g:i');
很简单的两行代码
$date = new DateTime();
echo $date->modify('-30 day')->format('y-m-d g:i');
回答by user3617709
From your brief description and example given, I believe that you want the date to be 30 days back and time to be the same as of now. The below code will serve this purpose. Thanks.
根据您给出的简要描述和示例,我相信您希望日期早于 30 天,时间与现在相同。下面的代码将用于此目的。谢谢。
<?php
$date=date('y-m-d g:i');
$time=date('g:i');
echo "Todays date:" . $date. "<br>";
$d2 = date('y-m-d', strtotime('-30 days'));
echo "30 days back:" . $d2 . ' ' .$time;
?>
回答by Anoop Saini
if you would like to get out put as 2014-08-01 then try the below code. thanks
如果您想在 2014-08-01 发布,请尝试以下代码。谢谢
$date = '2014-08-30 23:06';
$new_date = date('Y-m-d G:i', strtotime($date.' - 29 days'));
echo "30 days back is " . $new_date;
回答by Indrajeet Singh
回答by user1032531
I know you said with PHP, however, I can't imagine not getting the records from a DB. If you want to do so from the DB,use:
我知道你用 PHP 说的,但是,我无法想象不能从数据库中获取记录。如果您想从数据库中执行此操作,请使用:
$sql='SELECT * FROM myTable WHERE date > CURRENT_DATE - INTERVAL 30 DAY';
$pdo->query($sql);
回答by borodatych
Very simple one lines of code:
很简单的一行代码:
echo (new DateTime())->modify('-30 day')->format('y-m-d g:i');
In the example below, it makes no sense if the variable $dateis not used anywhere else!
在下面的例子中,如果变量$date没有在其他任何地方使用是没有意义的!
$date = new DateTime();
echo $date->modify('-30 day')->format('y-m-d g:i');