PHP strtotime returning always midnight - php

I'm having a weird problem. When I do strtotime it's not considering the hours part of the original date, and it's always returning midnight. I tried to research but I couldn't find anything specific.
Is there something I'm missing?
$original_date = "2015-08-07 02:00:00";
$next_date = date('Y-m-d H:i:s', strtotime("monday this Week +1 week", strtotime($original_date)));
It returns $next_date as 2015-08-14 00:00:00

Try this, add time which you want to retrieve in next date,.
$original_date = "2015-08-07 02:00:00";
echo $next_date = date('Y-m-d H:i:s', strtotime("monday this Week 02:00:00 +1 week", strtotime($original_date)));

monday this week +1 week assumes you’re looking for midnight of the monday of the week of the passed in time. If you want to preserve the hours part of the time, then you can append it to your date format because it should always be the same as in $original_date
date('Y-m-d ' . date('H:i:s', strtotime($original_date)), strtotime("monday this Week +1 week", strtotime($original_date)));

When you use monday in strtotime you're resetting the time back to 00:00:00. You will have to explicitly pass the time in either your date or strtotime to get your desired behavior. See this same question for a similar issue.
$next_date = date('Y-m-d H:i:s', strtotime("monday this Week +1 week " . date('H:i:s', strtotime($original_date)), strtotime($original_date)))

$date = strtotime('2018-08-14 02:00:00');
$next_date = date('Y-m-d H:i:s', strtotime("monday this Week 02:00:00 +1 week", $date)); // 2018-08-20 02:00:00

Related

Using next day in strtotime with PHP

It's monday and I run the following:
$d = date('d-m-Y', strtotime('this tuesday'));
Which gives me date the of tomorrow, 08-07-2014
If i run
$d = date('d-m-Y', strtotime('next tuesday'));
It provides me the same.
But how can i easiest get the next tuesday in the next week (not tomorrow)?
Just try with:
strtotime('this tuesday +1 week')
You can do the following
$d = date('d-m-Y', strtotime('next week tuesday'));
You can add days also to get desire result like :
date('Y-m-d', strtotime('this tuesday +7 days'));

How to get properly on Monday from a timestamp?

I am working with php5.5.1.
//date_default_timezone_get = UTC
date('d/m/Y ... H:i:s', 1400444640); // = Sunday 18/05/2014 ... 20:24
How do I get the current Monday of that week?
date('d/m/Y ... H:i:s', (strtotime('Monday this week', 1400444640))); // 19/05/2014 ... 00:00
Why do not I get a date Monday 12? How I can get it correctly?
If you want it to be the twelfth then you need to ask for the monday of the previous week.
date('d/m/Y ... H:i:s', (strtotime('previous week monday', 1400444640)));

PHP get next occurrence of Monday from a certain date (with time)

I'm looking for the next Thursday after a specific date, say 2014-02-25. The problem I'm having here is that when I use the below code, the time seems to be erased.
<?php
$timestamp = '2014-02-25 10:30:00';
echo date('Y-m-d H:i:s', strtotime("next Thursday", strtotime($timestamp)));
?>
The result I am getting is 2014-02-27 00:00:00 when I would like it to be 2014-02-27 10:30:00
Is there something I am missing here that is causing the time to be set to midnight?
I appreciate the help, thanks.
There is no time format that can directly express this. You need to produce a format like
next Thursday 10:30:00
... manually and pass that to strtotime(). The time information you need to extract from the reference time string. Like this:
$refdate = '2014-02-25 10:30:00';
$timestamp = strtotime($refdate);
echo date('Y-m-d H:i:s',
strtotime("next Thursday " . date('H:i:s', $timestamp), $timestamp)
);
The same results could be achieved using string concatenation:
echo date('Y-m-d', strtotime("next Thursday", $timestamp)
. ' ' . date('H:i:s', $timestamp);
The documentation for so called relative time formats can be found here

PHP How to find the date and time one year and one day ago when it's a leap year

I'm trying to get php to calculate a date that was one year and one day ago. I have this:
$date = date(strtotime('-366 days'));
$oneyear_oneday = date("Y-m-d H:i:s", $date);
$date = date(strtotime('-1 year'));
$oneyear = date("Y-m-d H:i:s", $date);
However, due to it being a leap year, both $oneyear and $oneyear_oneday provide the same output. Does anyone know how I can calculate this correctly?
ie if it's 3pm on 15th August 2012, I want the output to be 3pm on the 15th August 2011
with PHP5.3,
$date = new DateTime();
$interval = new DateInterval("P1Y");
$newdate = $date->sub($interval);
First, subtract one year. Then, subtract one day from the result:
$date = strtotime('-1 day', strtotime('-1 year'));
$oneyear_oneday = date("Y-m-d H:i:s", $date);
You can try to use mktime()...
Both calculations are correct. But if you want to get the same date, but one year before, you should simply use '-1 year'. The string '-366 days' is only correct in leap years.
$date = strtotime('2010-01-01 -1 year');
echo date('Y-m-d', $date);
The output stream looks like,
2009-01-01
Go this Link for more reference

php: strtotime("12/31/2004 +6 month")); not returning the last day of June

I expected this functional to return 6/30/2005 instead of 7/1/2005.
print date("m/d/Y", strtotime("12/31/2004 +6 month"));
Similarly, print date("m/d/Y", strtotime("1/31/2011 +1 month")) returns 03/03/2011 while would like it to return 2/28/2011.
Does anyone know if there is a straight forward way to show the last day of the added month?
How about this?
echo date("m/d/Y", strtotime("last day of 12/31/2004 + 6 month")); // 6/30/2005
echo date("m/d/Y", strtotime("last day of 1/31/2011 + 1 month")); // 2/28/2011
Demo
Edit: For your reference, here is a link to the documentation for relative times.
as strtotime continue in to next month if there isn't enoghe days that month,
you can back 6 month and check if its end up on the start date
$date2 = date("Y-m-d", strtotime("{$date} +6 months"));
$date3 = date("Y-m-d", strtotime("{$date2} -6 months"));
if($date3 != $date)
{
$date2 = date("Y-m-t", strtotime("{$date2} -1 months"));
}
(or in your case "m/t/Y")
One simple way is to actually go one month ahead of the day you want and then make the day value zero. Also, mktime() might be easier
$mymonth = 2; // I want the last day of February
echo date('m/d/Y', mktime(0,0,0,$mymonth+1,0,2011));
This should return 2/28/2011.
strtotime does the best it can with conflicting information. Saying
1/31/2011 +1month
would mean advancing to
2/31/2011
but February only has 28 (sometimes 29) days. 2011 isn't a leap year, so the "31st of February" gets normalized to "March 3rd".
The same applies for '12/31/2004 +6month'. That takes you to June 31st, 2005. But June only has 30 days, so the date is normalized to July 1st instead.

Categories