I'm trying to get the date 1 week before at 00:00:00 Here's what I've tried
$now = date("Y-m-d H:i:s");
$start_date_time = date('Y-m-d H:i:s', strtotime("-7 day"));
Output is 2017-04-11 11:33:52 (UTC)
I tried to use
$start_date_time ->setTime(0, 0);
echo $start_date_time ->format('H:i:s');
But it's not datetime so I get an error. Any tips?
you could use strtotime
$start_date_time = date('Y-m-d', strtotime('-7 days'));
or DateTime class
$date = new DateTime('7 days ago');
$date ->format('Y-m-d');
Just use DateTime and DateInterval.
Example:
$date = new DateTime();
$date->sub(new \DateInterval('P7D'));
echo $date->format('Y-m-d 00:00:00');
$d=strtotime("-7 Days");
echo date("Y-m-d 00:00:00", $d);
Related
I have the following code:
$now = date("Y-m-d H:m:s");
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));
However, now it gives me this error:
A non well formed numeric value encountered in...
why is this?
$date = (new \DateTime())->modify('-24 hours');
or
$date = (new \DateTime())->modify('-1 day');
(The latter takes into account this comment as it is a valid point.)
Should work fine for you here. See http://PHP.net/datetime
$date will be an instance of DateTime, a real DateTime object.
strtotime() expects a unix timestamp (which is number seconds since Jan 01 1970)
$date = date("Y-m-d H:i:s", strtotime('-24 hours', time())); ////time() is default so you do not need to specify.
i would suggest using the datetime library though, since it's a more object oriented approach.
$date = new DateTime(); //date & time of right now. (Like time())
$date->sub(new DateInterval('P1D')); //subtract period of 1 day
The advantage of this is that you can reuse the DateInterval:
$date = new DateTime(); //date & time of right now. (Like time())
$oneDayPeriod = new DateInterval('P1D'); //period of 1 day
$date->sub($oneDayPeriod);
$date->sub($oneDayPeriod); //2 days are subtracted.
$date2 = new DateTime();
$date2->sub($oneDayPeriod); //can use the same period, multiple times.
Carbon (update 2020)
Most popular library for processing DateTimes in PHP is Carbon.
Here you would simply do:
$yesterday = Carbon::now()->subDay();
you can do this in many ways...
echo date('Y-m-d H:i:s',strtotime('-24 hours')); // "i" for minutes with leading zeros
OR
echo date('Y-m-d H:i:s',strtotime('last day')); // 24 hours (1 day)
Output
2013-07-17 10:07:29
Simplest way to sub or add time,
<?php
**#Subtract 24 hours**
$dtSub = new DateTime('- 24 hours');
var_dump($dtSub->format('Y-m-d H:m:s'));
**#Add 24 hours**
$dtAdd = new DateTime('24 hours');
var_dump($dtAdd->format('Y-m-d H:m:s'));die;
?>
This may be helpful for you:
//calculate like this
$date = date("Y-m-d H:m:s", (time()-(60*60*24)));
//check the date
echo $date;
this should work, too
$date = date("Y-m-d H:m:s", strtotime('-24 hours'));
$now = date("Y-m-d H:i:s");
$date = date("Y-m-d H:i:s", strtotime('-24 hours', strtotime($now)));
Add "strtotime" before $now,
and Y-m-d H:m:s replace with Y-m-d H:i:s
You can simply use time() to get the current timestamp.
$date = date("Y-m-d H:m:s", strtotime('-24 hours', time()));
In same code use strtotime() its working.
$now = date("Y-m-d H:i:s");
$date = date("Y-m-d H:i:s", strtotime('-2 hours', strtotime($now)));
Try this :
$now = time();
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));
all you have to do is to alter your code to be
$now = strtotime(date("Y-m-d H:m:s"));
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));
I am using this code to generate yesterday's beginning of day in PST (aka America/Los_Angeles) time. I can't figure out how to convert the result to UTC.
date_default_timezone_set("America/Los_Angeles");
$time1 = date("Y-m-d H:i:s", mktime(0,0,0, date('n'), date('j')-1, date('Y')));
I tried this, but $time1 is not datetime, it's string. So the following won't work.
$time1->setTimezone(new DateTimeZone("UTC"));
The DateTime class can do all that for you
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles')); // will use now
echo $date->format('d/m/Y H:i:s'); //16/08/2016 16:13:29
$date->setTime(0,0,0);
$date->modify('-1 day');
echo $date->format('d/m/Y H:i:s'); // 15/08/2016 00:00:00
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('d/m/Y H:i:s'); // 15/08/2016 07:00:00
I have the following code:
$now = date("Y-m-d H:m:s");
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));
However, now it gives me this error:
A non well formed numeric value encountered in...
why is this?
$date = (new \DateTime())->modify('-24 hours');
or
$date = (new \DateTime())->modify('-1 day');
(The latter takes into account this comment as it is a valid point.)
Should work fine for you here. See http://PHP.net/datetime
$date will be an instance of DateTime, a real DateTime object.
strtotime() expects a unix timestamp (which is number seconds since Jan 01 1970)
$date = date("Y-m-d H:i:s", strtotime('-24 hours', time())); ////time() is default so you do not need to specify.
i would suggest using the datetime library though, since it's a more object oriented approach.
$date = new DateTime(); //date & time of right now. (Like time())
$date->sub(new DateInterval('P1D')); //subtract period of 1 day
The advantage of this is that you can reuse the DateInterval:
$date = new DateTime(); //date & time of right now. (Like time())
$oneDayPeriod = new DateInterval('P1D'); //period of 1 day
$date->sub($oneDayPeriod);
$date->sub($oneDayPeriod); //2 days are subtracted.
$date2 = new DateTime();
$date2->sub($oneDayPeriod); //can use the same period, multiple times.
Carbon (update 2020)
Most popular library for processing DateTimes in PHP is Carbon.
Here you would simply do:
$yesterday = Carbon::now()->subDay();
you can do this in many ways...
echo date('Y-m-d H:i:s',strtotime('-24 hours')); // "i" for minutes with leading zeros
OR
echo date('Y-m-d H:i:s',strtotime('last day')); // 24 hours (1 day)
Output
2013-07-17 10:07:29
Simplest way to sub or add time,
<?php
**#Subtract 24 hours**
$dtSub = new DateTime('- 24 hours');
var_dump($dtSub->format('Y-m-d H:m:s'));
**#Add 24 hours**
$dtAdd = new DateTime('24 hours');
var_dump($dtAdd->format('Y-m-d H:m:s'));die;
?>
This may be helpful for you:
//calculate like this
$date = date("Y-m-d H:m:s", (time()-(60*60*24)));
//check the date
echo $date;
this should work, too
$date = date("Y-m-d H:m:s", strtotime('-24 hours'));
$now = date("Y-m-d H:i:s");
$date = date("Y-m-d H:i:s", strtotime('-24 hours', strtotime($now)));
Add "strtotime" before $now,
and Y-m-d H:m:s replace with Y-m-d H:i:s
You can simply use time() to get the current timestamp.
$date = date("Y-m-d H:m:s", strtotime('-24 hours', time()));
In same code use strtotime() its working.
$now = date("Y-m-d H:i:s");
$date = date("Y-m-d H:i:s", strtotime('-2 hours', strtotime($now)));
Try this :
$now = time();
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));
all you have to do is to alter your code to be
$now = strtotime(date("Y-m-d H:m:s"));
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));
I want to add 5 minutes to this date: 2011-04-8 08:29:49
$date = '2011-04-8 08:29:49';
When I use strtotime I am always getting 1970-01-01 08:33:31
How do I add correctly 5 minutes to 2011-04-8 08:29:49?
$date = '2011-04-8 08:29:49';
$currentDate = strtotime($date);
$futureDate = $currentDate+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);
Now, the result is 2011-04-08 08:34:49 and is stored inside $formatDate
Enjoy! :)
Try this:
echo date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime('2011-04-8 08:29:49')));
$expire_stamp = date('Y-m-d H:i:s', strtotime("+5 min"));
$now_stamp = date("Y-m-d H:i:s");
echo "Right now: " . $now_stamp;
echo "5 minutes from right now: " . $expire_stamp;
Results in:
2012-09-30 09:00:03
2012-09-30 09:05:03
$date = '2011-04-8 08:29:49';
$newDate = date("Y-m-d H:i:s",strtotime($date." +5 minutes"))
For adding
$date = new DateTime('2014-02-20 14:20:00');
$date->add(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It add 5minutes
For subtracting
$date = new DateTime('2014-02-20 14:20:00');
$date->sub(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It subtract 5 minutes
If i'm right in thinking.
If you convert your date to a unix timestamp via strtotime(), then just add 300 (5min * 60 seconds) to that number.
$timestamp = strtotime($date) + (5*60)
Hope this helps
more illustrative for simple and clear solution
$date = '2011-04-8 08:29:49';
$newtimestamp = strtotime($date. ' + 5 minute');//gets timestamp
//convert into whichever format you need
$newdate = date('Y-m-d H:i:s', $newtimestamp);//it prints 2011-04-08 08:34:49
How can I get the timestamp of 12 o'clock of today, yesterday and the day before yesterday by using strtotime() function in php?
12 o'clock is a variable and would be changed by user.
$hour = 12;
$today = strtotime($hour . ':00:00');
$yesterday = strtotime('-1 day', $today);
$dayBeforeYesterday = strtotime('-1 day', $yesterday);
strtotime supports a number of interesting modifiers that can be used:
$hour = 12;
$today = strtotime("today $hour:00");
$yesterday = strtotime("yesterday $hour:00");
$dayBeforeYesterday = strtotime("yesterday -1 day $hour:00");
echo date("Y-m-d H:i:s\n", $today);
echo date("Y-m-d H:i:s\n", $yesterday);
echo date("Y-m-d H:i:s\n", $dayBeforeYesterday);
It works as predicted:
2011-01-24 12:00:00
2011-01-23 12:00:00
2011-01-22 12:00:00
OO Equivalent
$iHour = 12;
$oToday = new DateTime();
$oToday->setTime($iHour, 0);
$oYesterday = clone $oToday;
$oYesterday->modify('-1 day');
$oDayBefore = clone $oYesterday;
$oDayBefore->modify('-1 day');
$iToday = $oToday->getTimestamp();
$iYesterday = $oYesterday->getTimestamp();
$iDayBefore = $oDayBefore->getTimestamp();
echo "Today: $iToday\n";
echo "Yesterday: $iYesterday\n";
echo "Day Before: $iDayBefore\n";
You can easily find out any date using DateTime object, It is so flexible
$yesterday = new DateTime('yesterday');
echo $yesterday->format('Y-m-d');
$firstModayOfApril = new DateTime('first monday of april');
echo $firstModayOfApril->format('Y-m-d');
$nextMonday = new DateTime('next monday');
echo $nextMonday->format('Y-m-d');
to get start of day yesterday
$oDate = new DateTime();
$oDate->modify('-1 day');
echo $oDate->format('Y-m-d 00:00:00');
result
2014-11-05 00:00:00
All the answers here are too long and bloated, everyone loves one-lines ;)
$yesterday = Date('Y-m-d', strtotime('-1 day'));
(Or if you are American you can randomize the date unit order to m/d/y (or whatever you use) and use Cups, galloons, feet and horses as units...)
As of PHP 7 you can write something like this:
$today = new \DateTime();
$yesterday = (clone $today)->modify('-1 day');
$dayBefore = (clone $yesterday)->modify('-1 day');
// Then call ->format('Y-m-d 00:00:00'); on each objects
you can also use new DateTime("now") for today new DateTime("1 day ago") for yesterday or all can be parse by strtotime php function.
Then format as you want.
$timeStamp = time();
// $timeStamp = time() - 86400;
if (date('d.m.Y', $timeStamp) == date('d.m.Y')) {
echo 'Today';
} elseif (date('d.m.Y', $time) == date('d.m.Y', strtotime('-1 day'))) {
echo 'Yesterday';
}