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
Related
In my code, I get the current time and date value of the server following this organization parameter:
Y-m-d H:i:s Ex: 2018-09-01 22:35:18
I would like to remove 30 seconds of the time to use in the if comparison.
How can I accomplish this task?
Use strtotime function to add or subtract time.
If you want to subtract 30 seconds to current timestamp:
$thirtySecondsAgo = date('Y-m-d H:i:s', strtotime('-30seconds'));
If reference date is not NOW:
$date = "2018-08-15 22:51:47";
$dateThirtySecondsAgo = date('Y-m-d H:i:s', strtotime($date . '-30seconds'));
Try this for example current date minus 30 seconds:
$date = date('Y-m-d H:i:s', strtotime('-30 second'));
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
I am having trouble adding a time and time interval to a date.
I use a javascript datepicker that gives date in the form: 05/06/2013
I can convert this into a date time format with
$eventdate = date("Y-m-d H:i:s",strtotime($eventdate));
This gives: 2013-05-06 00:00:00
I get the time from a separate variable in minutes. Eight o'clock would be 480.
However, I cannot seem to find syntax to add 480 minutes to the date.
Have tried, for example,
echo date("Y/m/d h:i:s", strtotime("+480 minutes", $eventdate));
but this gives me the default 1969 date.
Thanks for any help or suggestions!
Use the DateTime class, it's easier to work with:
$eventdate = \DateTime::createFromFormat('Y/m/d h:i:s', $eventdate);
$eventdate->modify('+480 minutes');
echo $eventdate->format('Y/m/d h:i:s');
I need to display the time but it must start from 00:00:00? I've got the following but it uses the current time.
print(date("H:i:s"));
As an alternative to mktime(), try the newer DateTime class, eg
$dt = new DateTime;
$dt->setTime(0, 0);
echo $dt->format('H:i:s');
// Add one hour
$dt->add(new DateInterval('PT1H'));
echo $dt->format('H:i:s');
Update
The flexibility of DateInterval makes this a very good candidate for a timer, eg
// add 2 years, 1 day and 9 seconds
$dt->add(new DateInterval('P2Y1DT9S'));
Use mktime() if you want to start with midnight for the current date:
<?php
echo date('l jS \of F Y h:i:s A',mktime(0,0,0));
?>
OUTPUTS
Friday 14th of October 2011 12:00:00 AM
http://codepad.org/s2NrVfRq
In mktime(), you pass arguments for hours, minutes, seconds, month, day, year, so set hours, minutes, seconds to 0 to get today at midnight. (Note, as Phil points out, mktime()'s arguments are optional and you can leave month, day, year out and it will default to the current date).
The mktime() function returns a unix timestamp representing the number of seconds since the unix epoch (January 1, 1970). You can count up from it in seconds or multiples of seconds.
<?php
// $midnight = mktime(0,0,0,date('m'),date('d'),date('Y'));
// The above is equivalent to below
$midnight = mktime(0,0,0);
echo date('l jS \of F Y h:i:s A',$midnight)."\n";
echo date('l jS \of F Y h:i:s A',$midnight+60)."\n"; // One minute
echo date('l jS \of F Y h:i:s A',$midnight+(60*60))."\n"; // One hour
?>
OUTPUTS
Friday 14th of October 2011 12:00:00 AM
Friday 14th of October 2011 12:01:00 AM
Friday 14th of October 2011 01:00:00 AM
http://codepad.org/FTr98z1n
date() uses the current time when you don't pass in an explicit timestamp. See the optional argument in the date documentation.
If you want to explicitly format midnight, use:
date("H:i:s", mktime(0, 0, 0));
try to use this syntax:
print(date("H:i:s", 0));
or
print(date("H:i:s", 10)); // 10 seconds
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.