I use this function to set a time date("Y-m-d H:i:s", strtotime("+2 minutes"). Now I want to compare that value with the current time to find the amount of seconds it's left.
For example compare: $next_action = 2011-01-16 18:03:00 and $now = 2011-01-16 18:01:23. To find the amount of seconds.
strtotime can convert mysql timestamps to unix timestamps. so you just convert both of them to UNIX timestamps and subtract one from other, and you'll get the difference in seconds.
$next_action = "2011-01-16 18:03:00";
$now = "2011-01-16 18:01:23";
echo strtotime($next_action)-strtotime($now);
Why did you convert them to "Y-m-d H:i:s" in the first place? Unix timestamps are much easier to work with.
$start_time = date("Y-m-d H:i:s", strtotime("+2 minutes"));
$time_diff = (time() - strtotime($start_time)); // difference in seconds
$seconds = $time_diff % 60;
$minutes = ($time_diff - $seconds) % (60 * 60);
$hours = ($time_diff - ($minutes * 60) - $seconds) / (24 * 60 * 60);
Untested, but it would probably go something like this.
Related
I need to add a time stored in strong (format: hh:mm) to a date timestamp which before have no hours information (e.g. 2018-06-12 00:00:00). How to do it?
I would like to do this:
$startTime = "11:45";
$startDate = 1528754400; // 2018-06-12 00:00:00
$startDate = $startDate + $startTime; //2018-06-12 11:45:00
You can do some simple math and get there. Multiple minutes by 60 and hours by 3600 and you'll get the number of seconds the hours/minutes take up.
$startTime = "11:45";
$startDate = 1528754400;
$time = explode(':', $startTime);
$minutes = $time[1] * 60;
$hours = $time[0] * 3600;
Then just piece it back together. (the timestamp is the number of seconds since 1970 so just adding to it should be fine)
$startDate + $minutes + $hours
https://3v4l.org/U2jHL
An alternative approach would be using the datetime class.
$startTime = "11:45";
list($hours, $minutes) = explode(':', $startTime);
$date = new DateTime(date('Y-m-d', 1528754400));
$date->add(new DateInterval('PT' . $hours .'H' . $minutes . 'M'));
https://3v4l.org/D51XB
Just came across with this using strtotime:
$startTime = "11:45";
$startDate = 1528754400; // 2018-06-12 00:00:00
$startTimeTmp = explode(":",$startTime);
$finalTimestamp = strtotime("+".$startTimeTmp[0]." hours ".$startTimeTmp[1]." minutes", $startDate);
The future time is :2012-05-26 00:00:00
supposed there are three variable: $hour $minute $second
now, i want to using the future time subtract now time. then give the left hour to $hour,give the left minute to $minute,give the left second to $second.
i am sorry i am new of php, now i get stucked how to do the math operation ? thank you
A very good resource for dates and time..
http://www.php.net/manual/en/function.time.php
-there are samples here doing something similar.
Check the date_diff function. There's the exact solution to what you're asking there.
And here's the page (DateInterval::format) documenting how you can format the output.
$now = date_create();
// use "now" and necessary DateTimeZone in the arguments
$otherDate = date_create('2020-04-13');
$interval = date_diff($now, $futureDate);
echo $interval->format('%a days');
The following are the math operations for the difference in hours,minutes and seconds
$future_datetime = '2012-05-26 00:00:00';
$future = strtotime($future_datetime); //future datetime in seconds
$now_datetime = date('Y-m-d H:i:s');
$now = date('U'); //now datetime in seconds
//The math for calculating the difference in hours, minutes and seconds
$difference = $future - $now;
$second = 1;
$minute = 60 * $second;
$hour = 60 * $minute;
$difference_hours = floor($difference/$hour);
$remainder = $difference - ($difference_hours * $hour);
$difference_minutes = floor($remainder/$minute);
$remainder = $remainder - ($difference_minutes * $minute);
$difference_seconds = $remainder;
echo "The difference between $future_datetime and $now_datetime is $difference_hours hours, $difference_minutes minutes and $difference_seconds seconds";
I have a created_at date saved liked this "2011-09-23 19:10:18" And I want to get the days and hours left
until the date is reached. How do I do that? and column name in database remain days automatically update daily with remain days, please solve this
PHP fragment:
<?php
//Convert to date
$datestr="2011-09-23 19:10:18";//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)
//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));
//Report
echo "$days days $hours hours remain<br />";
?>
Note the hour-round and no minutes/seconds consideration means it can be slightly inaccurate.
This should seed your endeavor.
getdate(strtotime("2011-09-23 19:10:18"))
Full conversion:
$seconds = strtotime("2011-09-23 19:10:18") - time();
$days = floor($seconds / 86400);
$seconds %= 86400;
$hours = floor($seconds / 3600);
$seconds %= 3600;
$minutes = floor($seconds / 60);
$seconds %= 60;
echo "$days days and $hours hours and $minutes minutes and $seconds seconds";
as of PHP 5.3.0 you could use build-in Date object:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
http://www.php.net/manual/en/book.datetime.php
it would be something like
echo $date = date("Y-m-d H:i:s");echo "\n";
$original=time($date);
$modified = "2011-09-23 19:10:18";
echo date("Y-m-d H:i:s",$modified);echo "\n";
The easiest way is improved answer from CountZero. I used this solution for counter for time remaining before expiration of offer. Addition to first three lines of CountZero code:
$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
And now, you can use string functions to moderate your return values, to merge all or add '0' or '00' in front of the values.
You can find current date and time using date() function and then subtract
$x = 2011-09-23 - current_date
this will give you no. of days left.
Do same with time as well ..
Hope this helps
In PHP, how could I create a variable called $livetime that equals the current time minus 1 hour?
Another way - without all the math and, in my opinion, reads better.
$hour_ago = strtotime('-1 hour');
If you're looking for how to display the time in a human readable format, these examples will help:
$livetime = date('H:i:s', time() - 3600); // 16:00:00
$livetime = date('g:iA ', time() - 3600); // 4:00PM
$livetime = time() - 3600; // 3600 seconds in 1 hour : 60 seconds (1 min) * 60 (minutes in hour)
See time PHP function for more information.
convert your date to strtotime and then subtract one hour from it
$now = date('Y-m-d H:i:s');
$time = strtotime($now);
$time = $time - (60*60); //one hour
$beforeOneHour = date("Y-m-d H:i:s", $time);
You could use the date_create function along with the date_sub function like I have shown here below: -
$currentTime = date_create(now());
$modifyTime = date_sub($currentTime,date_interval_create_from_date_string("1 hour"));
$liveTime = $modifyTime->format('Y-m-d H:i:s');
Assuming that a timestamp is fine you can use the time function like so
<?php
$livetime = time() - 60 * 60;
Current time is equal to time() (current time given in seconds after Unix epoch).
Thus, to calculate what you need, you need to perform calculation: time() - 60*60 (current time in seconds minus 60 minutes times 60 seconds).
$time_you_need = time() - 60*60;
First convert hours into seconds (3600) then use the following:
$your_date = date('F jS, Y',time() - 3600);
I have two dates, formated like "Y-m-d H:i:s". I need to compare these two dates and figure out the hour difference.
You can convert them to timestamps and go from there:
$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
Dividing by 3600 because there are 3600 seconds in one hour and using round() to avoid having a lot of decimal places.
You can use DateTime class also -
$d1= new DateTime("06-08-2015 01:33:26pm"); // first date
$d2= new DateTime("06-07-2015 10:33:26am"); // second date
$interval= $d1->diff($d2); // get difference between two dates
echo ($interval->days * 24) + $interval->h; // convert days to hours and add hours from difference
As an addition to accepted answer I would like to remind that \DateTime::diff is available!
$f = 'Y-m-d H:i:s';
$d1 = \DateTime::createFromFormat($date1, $f);
$d2 = \DateTime::createFromFormat($date2, $f);
/**
* #var \DateInterval $diff
*/
$diff = $d2->diff($d1);
$hours = $diff->h + ($diff->days * 24); // + ($diff->m > 30 ? 1 : 0) to be more precise
\DateInterval documentation.
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;
You can try this :
$time1 = new DateTime('06:56:58');
$time2 = new DateTime('15:35:00');
$time_diff = $time1->diff($time2);
echo $time_diff->h.' hours';
echo $time_diff->i.' minutes';
echo $time_diff->s.' seconds';
Output:
8 hours 38 minutes 2 seconds
The problem is that using these values the result is 167 and it should be 168:
$date1 = "2014-03-07 05:49:23";
$date2 = "2014-03-14 05:49:23";
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;
$date1 = date_create('2016-12-12 09:00:00');
$date2 = date_create('2016-12-12 11:00:00');
$diff = date_diff($date1,$date2);
$hour = $diff->h;
This is because of day time saving.
Daylight Saving Time (United States) 2014 began at 2:00 AM on
Sunday, March 9.
You lose one hour during the period from $date1 = "2014-03-07 05:49:23" to
$date2 = "2014-03-14 05:49:23";
You can try this:
$dayinpass = "2016-09-23 20:09:12";
$today = time();
$dayinpass= strtotime($dayinpass);
echo round(abs($today-$dayinpass)/60/60);
You can use strtotime() to parse your strings and do the difference between the two of them.
Resources :
php.net - strtotime()