I have two dates:
$today = '2012-12-01 10:40:00';
$check = '2012-12-03 12:00:00';
How can I show countdown for this dates?
Should show me:
Count: 49 hours and 20 minutes. I can check only hours or only minutes with function mktime, but how can i compare this?
try using DateTime::diff
<?php
$datetime1 = new DateTime('2012-12-01 10:40:00');
$datetime2 = new DateTime('2012-12-03 12:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%1 day %h hours %i minutes');
?>
You can change these dates to a unix timestamp with strtotime.
Then you can calculate the difference between them in seconds.
60 seconds in a minute, 60 minutes in an hour.
Assuming you want the clock to keep ticking as the user stays on the page, you don't really want to do that using PHP (unless you want to dispatch AJAX calls to the server every second to update the clock, which would suck). Do it client-side, using javascript.
Here are 25 pretty scripts that do that using jQuery: http://www.tripwiremagazine.com/2012/11/jquery-countdown-scripts.html
Related
I have this PHP code which calculates the time between 2 timestamps and displays it in minutes...
$timestamp1 = new DateTime();
$timestamp1->setTimestamp('1540718680');
$timestamp2 = new DateTime();
$timestamp2->setTimestamp('1540747360');
$since_start = $timestamp1->diff($timestamp2);
echo $since_start->i.' minutes<br>';
For some reason it is returning 58 minutes insead of 477 minutes.
Where am I going wrong?
%i gives only the minutes, it's like asking for the 10s digit in a 3 digit number. You also need the hours since minutes is just the remainder after the larger units are subtracted.
You could also do the computation yourself, b-a/60, to get the number of minutes between the two specified timestamps.
I saw this response to a thread and it works but doesn't create a range between the current day and seven days time. Which is what I need. Can anyone give me a hand please?
Update:
An easier way to word it. I want to Select data between two dates. For example current day and seven days time.
My current code:
if($current_day) {
$data['current_day']=date('Y-m-d', strtotime('+7 days'));
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours, %i minutes, %s seconds");
You can use this code sample if you have php 5.3 or above,
Else try to calculate the difference of two dates in seconds using time() and strtotime(). Then translate those seconds into days/hours/minutes/seconds.
I run this script which works sweet estimating how much time before something weather related happens. But a midnight it goes crazy and for the whole midnight hour, it returns crazy negative times like -1100 minutes and stuff, then when it gets to 0100 hrs it's back to normal and reports, like 20 mintues etc.
Script:
$timenow = date("H:i:s");
$eventtime= strtotime("$gettimefromtextfile"); //time the weather event will happen in the near future
$TimeEnd = strtotime($timenow);
$Difference = ($eventtime - $TimeEnd);
if ($Difference >= 0) {
$minutes = floor(($Difference / 60));
// print how many minutes until an event happens, discard it if event is in the past
I know the date function had issues with midnight up to PHP 5.3. But I am running PHP 5.3 so shouldn't be an issue. I don't need the date, it is only time I need, weather related stuff is reported only hours difference at most.
Any suggestions on an alternative function or coding that will stop this spasm at midnight?
What about using DateTime::diff? Don't reinvent the wheel!
<?php
date_default_timezone_set('Europe/Lisbon');
$next = new DateTime('18:00:01');
$now = new DateTime();
$diff = $next->diff($now);
echo $diff->format('%h hours, %i minutes');
?>
Reference: http://php.net/manual/en/datetime.diff.php
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
I have 2 unix time, one is todays date and other is expiration time.
Todays Time: 1377173245 (2013-08-22 12:07:25)
Expiration Time: 1406303166 (2014-07-25 15:46:06)
What I want to achieve is calculate remaining time to expire the listing. In current case, it should show, 11 months -- hours -- minutes -- seconds I didn't find any good solution to calculate the difference. I doubt is it possible to calculate time difference using UNIX time system?
Thank you :-)
This is what you're looking for: DateTime::diff
First create object with 1st date:
$date = new DateTime('2013-08-22 12:07:25');
Then use the diff method:
$diff = $date->diff(new DateTime('2014-07-25 15:46:06'));
Now if you print $diff variable, you will see the array containing values for hour, minute, day and so on.
Use DateTime::diff
See the doc on https://www.php.net/manual/en/datetime.diff.php
Try using datetime::diff. Here's the example from the php.net documentation:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
It's easy to create a DateTime object using a timespamp. However, this solution requires PHP 5.3+.
Use DateTime class:
Example:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2014-07-25 15:46:06');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%M months, %H hours, %I minutes, and %S seconds remaining ');
Outputs:
11 months, 21 hours, 44 minutes, and 20 seconds remaining
It is most definitely possible. However, you'll run into problems with months, because there are different number of days in each month, so I'll just do days, hours, minutes, and seconds below.
$days=($expiration-$today)/(60*60*24);
$hours=(($expiration-$today)/(60*60))%24; //the modulus operator, gets a remainder
$minutes=(($expiration-$today)/(60))%(60*60);
$seconds=($expiration-$today)/60;
If you need months, you'll find it easier to use date_diff().
I have time in minutes and I want to find out how many hours it is. But with attached code, for 1600 minutes, I get 2 hours and 40 minutes. I need it in format 26:40:00. Thanks for help.
$my_time = 1600;
echo date("H:i:s", $my_time);
Try using a DateInterval object, which is specifically built to handle intervals of time:
$interval = new DateInterval('M1600');
echo $interval->format('%H:%i:%s');
date isn't really suited for this. Instead, try this:
echo sprintf("%s:%2s:%2s",floor($my_time/3600),floor($my_time/60)%60,$my_time%60);
(This is assuming you have $my_time in seconds, not minutes. Multiply by 60 up front to get the time in seconds)