How to calculate the date of next Monday in PHP? - php

I'm trying to get a date that comes next Monday at night 00:01 am. I'm using the following lines:
date('Y-m-d h:i:s', strtotime(**'next monday, 01:01am'**, strtotime(date("Y-m-d h:i:s"))))
This gives me the proper output:
2014-05-19 01:01:00
But if I try to write:
date('Y-m-d h:i:s', strtotime(**'next monday, 00:01am'**, strtotime(date("Y-m-d h:i:s"))))
I'm getting:
1970-01-01 05:30:00
Please tell me what is wrong here.

00:01 am is not correct Use 12:01 am
Try this:
date('Y-m-d h:i:s', strtotime('next monday, 12:01am', strtotime(date("Y-m-d h:i:s"))));
It will give you 2014-05-19 12:01:00
Demo

Related

Converting remaining time to actual date and time

I have this time format.
For example: 04d 03h 15m,
when obviously is the time left for something to end or expire. How can I convert that to an actual date and time from now using PHP. As in this example, Say today is 19/1/2017 5:45am, 4 days from now is going to be 23rd Jan 2017, 3:15 am.
Thanks in anticipation.
you can do that by using Date() and time() with strtotime() and Try this code
$currenttime = time();
echo date('d/m/y H:i A', $currenttime);
$timeAfterAdding = strtotime("+4 days +3 hours +15 minutes", $currenttime);
$expireTime = date('d/m/y H:i A', $timeAfterAdding);
echo $expireTime;
output:
current time => 19/01/17 05:12 AM
expire time => 23/01/17 09:27 AM

PHP strtotime returning always midnight

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

Convert date time with AM PM to 24 hour date format

How to convert 10/25/2014 14:00 PM to 2014-10-25 14:00:00 in php?
date('Y-m-d H:i:s', strtotime("10/25/2014 14:00 PM"));
returns
1970-01-01 03:00:00
I don't know what the origin of that date is (how it came to that), but you can use DateTime classes for this:
$raw_date = '10/25/2014 14:00 PM';
// to disregard that PM escape it in the format
$new_date = DateTime::createFromFormat('m/d/Y H:i \P\M', $raw_date);
echo $new_date->format('Y-m-d H:i:s'); // 2014-10-25 14:00:00
$date = "10/25/2014 14:00 PM"; //Here is the date 24 hours format with am/pm
$date = substr($date, 0, -2); //Removed the am/pm from date
echo date('Y-m-d H:i:s', strtotime($date)); //then convert it to mysql date format
Note:
If you use this for date column means replace it data type to int and save unix time stamp which have more comfortable if year limited to 1970 to year 2038.
For reason not to use more than 2038 read here
10/25/2014 14:00 PM your input date format is not correct, you are using 12hr format so time will be 10/25/2014 02:00 PM

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)));

How to convert time like 2 pm or 3 am to HH:MM:SS format in php?

How can I convert times like 2 pm or 3 am to HH:MM:SS format in php?
Like this:
$date = '2pm';
echo date('H:i:s', strtotime($date));
Result:
14:00:00
And:
$date = '2am';
echo date('H:i:s', strtotime($date));
Result:
02:00:00
More Info:
date
strtotime
Update:
To convert it back:
$date = '14:00:00';
echo date('HA', strtotime($date));
Result:
14PM
(this should be a comment, but it's too long)
The reason Sarfraz's solution is incomplete is because it doesn't account for DST transaitions. During DST transitions, some hours may not exist.
Consider the timezone is Europe/Lisbon and we're in March 28th 2010, when DST kicked in.
When we hit 1am, we change from UTC+0 to UTC+1, i.e., we skip 1 hour. Example:
date_default_timezone_set("Europe/Lisbon");
$date = '2010-03-28 1am';
$date2 = '2010-03-28 1:30am';
echo date('H:i:s', strtotime($date)),"\n";
echo date('H:i:s', strtotime($date2)),"\n";
gives
02:00:00
02:30:00
Therefore, Sarfraz solution will fail unless when you say you want to convert 1am to 01:00, these times always refer to the current day in the server's timezone.

Categories