I have to admit that having not even tried to code this myself this question may be a annoying to some but I'm very surprised that I wasn't able to find a good example on the web of what I'm trying to do. Perhaps I'm just not using the right key words in my searches.
I am trying to calculate the time remaining from now (the time the page loads) until a specific date and time (let's say Wednesday May 11, 2011 12:00PM for example) and display it. I had thought it would be quite easy to find a snippet to accomplish this but no luck so far. Does anyone have some example code they have used to accomplish this before? I don't expect anyone to write this for me from scratch but if I can at least get pointed in the right direction it would be extremely helpful.
I'd use the DateInterval and DateTime functions:
$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'll need a version of PHP that's at least 5.3 to do it this way - otherwise, do what helloandre recommends.
I think it will usefull
$startdate="2008-06-22 20:38:25";
$enddate="2008-06-29 21:38:49";
$diff=strtotime($enddate)-strtotime($startdate);
echo "diff in seconds: $diff<br/>\n<br/>\n";
// immediately convert to days
$temp=$diff/86400; // 60 sec/min*60 min/hr*24 hr/day=86400 sec/day
// days
$days=floor($temp); echo "days: $days<br/>\n"; $temp=24*($temp-$days);
// hours
$hours=floor($temp); echo "hours: $hours<br/>\n"; $temp=60*($temp-$hours);
// minutes
$minutes=floor($temp); echo "minutes: $minutes<br/>\n"; $temp=60*($temp-$minutes);
// seconds
$seconds=floor($temp); echo "seconds: $seconds<br/>\n<br/>\n";
echo "Result: {$days}d {$hours}h {$minutes}m {$seconds}s<br/>\n";
echo "Expected: 7d 0h 0m 0s<br/>\n";
echo "time isss".time();
echo $date = date('Y-m-d H:i:s')";
?>
first you'll need to calculate the difference in seconds using time() and strtotime(). Then you can translate those seconds into days/hours/minutes/seconds.
Related
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
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)
In my MySQL table I have a datatime column (the time is saved in the following format 2011-02-15 11:01:14). I extract this information with the PHP and print it to the user page.
$results = mysql_query($cmd,$link);
while ($res_array = mysql_fetch_array($results)) {
$submitted_at = $res_array['datetime'];
print $submitted_at."<br>";
}
However, I would like to show not only the given datatime, but also how long ago it was. In more detail, I need something like that: (67 day 4 hours 47 minutes 7 seconds ago). Does PHP have some functions that can make it easier. In particular I bother with the fact that month could have 28, 29, 30 and 31 days.
As an alternative, I thought about doing it on the MySQL level. It should be easy to extract the number of second from the given time to the current data. Then I can easilly transform seconds into days, hours and minutes. But I do not know how can I access this information using the mysql_fetch_array. Should it be something like $res_array['unix_timestamp(now())-unix_timestamp(datatime)']?
This'll be what you're looking for just change the formatting to suit your needs.
http://php.net/manual/en/datetime.diff.php
For example:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
Change this line:
$interval->format('%R%a days')
Following guidelines from here:
http://php.net/manual/en/function.date.php
Edit: This function is PHP 5.3+
Edit2 : Found this for older versions of PHP
How to calculate the difference between two dates using PHP?
Hi I get an error when trying to get date interval using php strtotime function
the code is:
<?php
$interval = time() - strtotime('1992/08/13');
//expect to be 18
// but the output is 1988
print date('Y', $interval);
?>
any advice?
thanks
If you want to deal with date intervals in PHP I can't recommend the DateInterval class enough. I wrote a blog post on this earlier this week: Working with Date and Time in PHP
There's an example of using it there that should allow you to do what you want to do.
That is because all time() functions are seconds since epoch which is in 1970, so your out is actually 18 years since epoch. If you want it to get the difference in years you will probably have to calculate the difference yourself.
print $interval / (60*60*24*365.242199);
Are you tring to get the years elapsed rather than the actual year?
If so:
$year = 31556926;
$interval = time() - strtotime('1992/08/13');
print round($interval / $year);
$interval = time() - strtotime('1992/08/13');
These PHP functions deal with UNIX timestamps. That means the number of seconds from 1970. 01. 01. So 1992/08/13 is transformed into a timestamp (seconds). time() gives the current timestamp (seconds). You subtract the former from the latter, and you get the amount of seconds between those two dates. This is not a date itself, just an interval.
If you want to get the year, do something like echo $interval/(60*60*24*365); which will convert your seconds to years (not accurate, leap years will not be taken into consideration). Though your best option is checking out #James C's link and use his solutions. I just wanted to give some explanation.