I want to have a countdown to a unix timestamp:
Where it counts down in seconds
Like this:
Expires in: 1d 10h 52m 25s
Heres an example timestamp:
1303725600
How could I do that?
I think this would work...
$tDiff = 1303725600 - time();
$days = floor($tDiff / 86400);
$hours = ($tDiff / 3600) % 24;
$mins = ($tDiff / 60) % 60;
$secs = ($tDiff) % 60;
I just made one of those. http://chrischerry.name/coachella
Check out the source for some ideas on how to do it. The "destination" date however isn't specified by a unix time stamp but that's easy enough to do.
var date = new Date(unix_timestamp*1000);
Its multiplied by 1000 because the javascript Date() wants the time in milliseconds, and unixtimestamps are in seconds.
Related
So pretty much I'm storing the amount of play time a player has in milliseconds and I need to convert it to the amount of time it equals (string).
I've already tried it but I can't seem to get it to be accurate. I used rounding and it turned out poorly.
Can anybody help me out?
Example: 183547165 -> * days * hours * minutes * seconds
If I'm reading the question right, then I think you want something like this!
<?php
$milliseconds = '183547165';
$time = $milliseconds / 1000;
$days = floor($time / (24*60*60));
$hours = floor(($time - ($days*24*60*60)) / (60*60));
$minutes = floor(($time - ($days*24*60*60)-($hours*60*60)) / 60);
$seconds = ($time - ($days*24*60*60) - ($hours*60*60) - ($minutes*60)) % 60;
echo $days.' days<br>'.$hours.' hours<br>'.$minutes.' minutes<br>'.$seconds.' seconds';
?>
Converts milliseconds to days, hours, minutes, and seconds.
PHP has a date function which do what you want:
date("H:i:s", '183547165');
It outputs:
09:19:25
PHP DATE
Calculating time using date and strtotime functions.
$starttime = '11:55';
$endtime = '13:01'; //or '1:01'
$totaltime = date("i",strtotime($endtime) - strtotime($starttime));
I don't know why, but echo $totaltime giving 06 instead of 66
Its working fine on other time frames. i.e for 12:30, 13:30
Thanks for any help.
Your return value is formatted as a new time output (because of the date() function)
As you are only requesting the number of minutes in the formatted time, it's only returning that part.
If you want to see the hours and minutes, output using h:
$totaltime = date("H:i",strtotime($endtime) - strtotime($starttime));
This will return "01:06".
If you need the actual number of minutes between two dates (so you want 66 as outcome) then you're not looking for a formatted time-string as an outcome, but rather a regular integer holding the number of minutes. This you can calculate from your earlier calculation, like this:
// divide total seconds between these points by 60, round down.
$totalMinutes = floor ( ( strtotime($endtime) - strtotime($starttime) ) / 60 );
Because date("i") only shows minutes from 0 to 59.
PHP date manual
A possible solution:
$totaltime = (strtotime($endtime) - strtotime($starttime)) / 60;
<?php
$starttime = '11:55';
$endtime = '13:01';
echo $totaltime (strtotime($endtime) - strtotime($starttime)) / 60;
echo $totaltime = floor ((strtotime($endtime) - strtotime($starttime)) / 60);
?>
I have converted the length of the movie Avatar from 2009, from minutes to timestamp. The movie is 162 minutes long so the timestamp is 1386227800. Now I need to convert the timestamp to hours and minutes which I don't know how.
In short: how can I convert a timestamp and get the correct result in hours and minutes?
I have tested floor(1386227800 / 60), date('H:i', mktime(0, 1386227800) and some functions that converts a timestamp to hours and minutes, but these only converts the hours to something endless, like 12375 or something like that.
So, how can I accomplish this?
As one of the commenters mentioned, a timestamp represents a single point in time, not a duration. There's no reason to call strtotime at all -- if you already have the total minutes, you can ignore converting it to a timestamp and just get that into hours:minutes like this:
$time=162;
$hours = floor($time / 60);
$minutes = ($time % 60);
echo $hours.":".$minutes;
echo date('H:i:s', 1386227800);
Can you try,
echo date('H:i','1386227800');
As I understand it, you want to convert 162 minutes to be represented with hours and minutes. This is a simple case of mathematics.
<?php
$minutes = 162 % 60;
$hours = floor(162 / 60);
?>
$hours will return 2, and $minutes will return 42. 2 hours, 42 minutes is equivalent to 162 minutes.
Hope this helps.
I need to somehow take a unix timestamp and output it like below
Can this be done with MySQL? Or php
Mike 7s ago
Jim 44s ago
John 59s ago
Amanda 1m ago
Ryan 1m ago
Sarah 1m ago
Tom 2m ago
Pamela 2m ago
Ruben 3m ago
Pamela 5h ago
As you can guess i only wanna print the minute, not minutes and seconds(1m 3s ago)
What should I look into?
Yes it can be done. See related post
$before // this is a UNIX timestamp from some time in the past, maybe loaded from mysql
$now = time()
$diff = $now - $before;
if( 1 > $diff ){
exit('Target Event Already Passed (or is passing this very instant)');
} else {
$w = $diff / 86400 / 7;
$d = $diff / 86400 % 7;
$h = $diff / 3600 % 24;
$m = $diff / 60 % 60;
$s = $diff % 60;
return "{$w} weeks, {$d} days, {$h} hours, {$m} minutes and {$s} secs away!"
}
PHP 5.3 and newer have DateTime objects that you can construct with data coming back from a database. These DateTime objects have a diff method to get the difference between two dates as a DateInterval object, which you can then format.
Edit: corrected sub to diff.
Edit 2:
Two catches with doing it this way:
DateTime's constructor doesn't appear to take a UNIX timestamp... unless prefixed with an #, like this: $startDate = new DateTime('#' . $timestamp);
You won't know what the largest unit is without manually checking them. To get an individual field, you still need to use format, but with just a single code... Something like $years = $dateDiff->format('y');
function sECONDS_TO_DHMS($seconds)
{
$days = floor($seconds/86400);
$hrs = floor($seconds / 3600);
$mins = intval(($seconds / 60) % 60);
$sec = intval($seconds % 60);
if($days>0){
//echo $days;exit;
$hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
$hours = $hrs-($days*24);
$return_days = $days." Days ";
$hrs = str_pad($hours,2,'0',STR_PAD_LEFT);
}else{
$return_days="";
$hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
}
$mins = str_pad($mins,2,'0',STR_PAD_LEFT);
$sec = str_pad($sec,2,'0',STR_PAD_LEFT);
return $return_days.$hrs.":".$mins.":".$sec;
}
echo sECONDS_TO_DHMS(2); // Output 00:00:02
echo sECONDS_TO_DHMS(96000); // Output 1 Days 02:40:00
PHP's date() function
as well as time() and some others that are linked in those docs
This can also be done in Mysql with date and time functions
You can try my Timeago suggestion here.
It can give outputs like this:
You opened this page less than a
minute ago. (This will update every
minute. Wait for it.)
This page was last modified 11 days
ago.
Ryan was born 31 years ago.
I dont have a mysql server at hand, but a combination of the following commands should get you something like what you want.
DATEDIFF
AND
DATEFORMAT
2009-10-05 18:11:08
2009-10-05 18:07:13
This should generate 235,how to do it ?
With DateTime objects, you can do it like this:
$date = new DateTime( '2009-10-05 18:07:13' );
$date2 = new DateTime( '2009-10-05 18:11:08' );
$diffInSeconds = $date2->getTimestamp() - $date->getTimestamp();
You can use strtotime() to do that:
$diff = strtotime('2009-10-05 18:11:08') - strtotime('2009-10-05 18:07:13')
A similar approach is possible with DateTime objects, e.g.
$date = new DateTime( '2009-10-05 18:07:13' );
$date2 = new DateTime( '2009-10-05 18:11:08' );
$diff = $date2->getTimestamp() - $date->getTimestamp();
PHP Date Time reference is helpful for things like this: PHP Date Time Functions
strtotime() is probably the best way.
$seconds = strtotime('2009-10-05 18:11:08') - strtotime('2009-10-05 18:07:13')
For those worrying about the limitations of using timestamps (i.e. using dates before 1970 and beyond 2038), you can simply calculate the difference in seconds like so:
$start = new DateTime('2009-10-05 18:11:08');
$end = new DateTime('2009-10-05 18:07:13');
$diff = $end->diff($start);
$daysInSecs = $diff->format('%r%a') * 24 * 60 * 60;
$hoursInSecs = $diff->h * 60 * 60;
$minsInSecs = $diff->i * 60;
$seconds = $daysInSecs + $hoursInSecs + $minsInSecs + $diff->s;
echo $seconds; // output: 235
Wrote a blog post for those interested in reading more.
Because of unix epoch limitations, you could have problems compairing dates before 1970 and after 2038. I choose to loose precision (=don't look at the single second) but avoid to pass trough unix epoch conversions (getTimestamp). It depends on what you are doing to do...
In my case, using 365 instead (12*30) and "30" as mean month lenght, reduced the error in an usable output.
function DateIntervalToSec($start,$end){ // as datetime object returns difference in seconds
$diff = $end->diff($start);
$diff_sec = $diff->format('%r').( // prepend the sign - if negative, change it to R if you want the +, too
($diff->s)+ // seconds (no errors)
(60*($diff->i))+ // minutes (no errors)
(60*60*($diff->h))+ // hours (no errors)
(24*60*60*($diff->d))+ // days (no errors)
(30*24*60*60*($diff->m))+ // months (???)
(365*24*60*60*($diff->y)) // years (???)
);
return $diff_sec;
}
Note that the error could be 0, if "mean" quantities are intended for diff. The PHP docs don't speaks about this...
In a bad case, error could be:
0 seconds if diff is applied to time gaps < 1 month
0 to 3 days if diff is applied to time gaps > 1 month
0 to 14 days if diff is applied to time gaps > 1 year
I prefer to suppose that somebody decided to consider "m" as 30 days and "y" as 365, charging "d" with the difference when "diff" walk trough non-30-days months...
If somebody knows something more about this and can provide official documentation, is welcome!
strtotime("2009-10-05 18:11:08") - strtotime("2009-10-05 18:07:13")
The solution proposed by #designcise is wrong when "end date" is before "start date".
Here is the corrected calculation
$diff = $start->diff($end);
$daysInSecs = $diff->format('%r%a') * 24 * 60 * 60;
$hoursInSecs = $diff->format('%r%h') * 60 * 60;
$minsInSecs = $diff->format('%r%i') * 60;
$seconds = $daysInSecs + $hoursInSecs + $minsInSecs + $diff->format('%r%s');
A simple and exact solution (exemplifying Nilz11's comment):
$hiDate = new DateTime("2310-05-22 08:33:26");
$loDate = new DateTime("1910-11-03 13:00:01");
$diff = $hiDate->diff($loDate);
$secs = ((($diff->format("%a") * 24) + $diff->format("%H")) * 60 +
$diff->format("%i")) * 60 + $diff->format("%s");