How can i display seconds ago/minutes ago/with unix timestamp? - php

I need the unix timestamp - the timestamp i have. Then display the time between like on twitter.

If you have the difference called diff:
$seconds = intval($diff) % 60;
$minutes = intval($diff/60) % 60;
$hours = intval($diff/3600) % 24;
$days = intval($diff/(3600*24));
Is this what you want ?

Not sure what language you need it, but if it will end up in a web page, you may try timeago.

Use example :
echo time_elapsed_string('#1367367755');
echo time_elapsed_string('#1367367755', true);
Output :
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Link to the function.

Related

PHP get time different in string

I have a time string, the first user's last login : '2014.12.01, 12:25'
And how to calculate the time different from now? For e.g. another user see the first user's last login
At 12:30 and it says 5 minute ago.
Or at 13:23 says 48 minute ago.
Or at 14:24 says 1 hour ago.
Or one day later, it says 1 day ago.
You can try the following:
$to_time = strtotime("2014-12-01 12:25:00");
$from_time = strtotime("2014-12-01 12:25:00");
$minutes = round(abs($to_time - $from_time) / 60,2);
$seconds = abs($to_time - $from_time) % 60;
echo "$minutes minute, $seconds seconds";
It had already been answered at the following link:
"Calculate time different in minute and second"
Hope this helps
$first_user_time = '2014-12-01 12:30:00';
$curnt_user_time = '2014-12-01 12:40:00';
$diff = date('h:i:s', strtotime($first_user_time)-strtotime($curnt_user_time))
echo $diff;
//it will give diff in hour : min : seconds

Time difference php/mysql need to format the output conditionally (4 small highlights required)

im trying to get difference of date/time from a field type datetime to "right now" using php and mysql as database
this code is working fine, returns the output beautifully ok as required
$datetime1 = new DateTime('mydate1');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%d days %h hours %i minutes');
that is ok so far, no issues as this function is for php 5.3 and i have it on server
my need is 4 small things actually
1) how to eliminate the need for days?
i want to have (25 hours 10 minutes) instead of (1 day 1 hours 10 minutes)
2) how i can make $elapsed be bold or colored if the value is more than 5 hours for example!? simple IF logic will not work as the output is not actually a predefined value...
3) if the days or hours are 0, then want to remove them!
- For example if showing (0 days 10 hours 40 mins) then no need to display the (0 days), should show (10 hours 40 mins) that is enough
- Another example: 0 days 0 hours 45 minutes then to show only "45 minutes" no need for days and hours!
4) if output less than 5 minutes in total (0days 0 hours 1-5mins), then wanna make it show like "a while ago" only no need for any days, hours or minutes... then after 6 minutes.. go like "6 mins"
shortly something like facebook!?
okay, what i searched tried is different combination of workarounds but never worked as you know this interval is for php 5.3 and still seem not widely used?
any hint for one or more parts of this long question is appreciated,
M, Derik
tried to answer all your questions. so read the comments because i didnt write numbers for the problems. sorry
$datetime1 = new DateTime('mydate1');
$datetime2 = new DateTime();
$minutes = round(abs($datetime1 - $datetime2) / 60,2); //to calculate total time in MINUTES
if($minutes < 5) // for awhile ago problem.
{
return "awhile ago";
}
elseif($minutes > 6 && < 60)
{
return $minutes." minutes ago"; //for 6 minutes and after.
}
elseif($minutes>60) // for the hours..
{
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
$string = $hours. " hours and" . $minutes . " minutes ago.";
if($hours>5) //for bolding characters after
{
return "<b>".$hours." hours and".$minutes." minutes ago.</b>"; //for bolding character
}
else
return $hours." hours and".$minutes." minutes ago.";
}
hope this helps.

Convert timestamp to hours and minutes for a movies' length

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.

How to return the amount of years passed?

My friend and I are working on a fairly basic uptime script for an IRC Bot.
Here's our code:
function Uptime()
{
global $uptimeStart;
$currentTime = time();
$uptime = $currentTime - $uptimeStart;
$this->sendIRC("PRIVMSG {$this->ircChannel} :Uptime: ".date("z",$uptime)." Day(s) - ".date("H:i:s",$uptime));
}
$uptimeStart is set immediately when the script runs, as time();
for some reason when I execute this function, it starts at 364 days and 19 hours. I can't figure out why.
Your $uptime is not a timestamp as should be used in date(), but a difference in time. You have an amount of seconds there, not a timestamp (that corresponds with an actual date.
just use something like this to cacluate (quick one, put some extra brain in for things like 1 day, 2 hours etc) ;)
$minutes = $uptime / 60;
$hours = $minuts/60 ;
$days = $hours / 24
etc
If you have 5.3 or above, use the DateTime and DateInterval classes:
$uptimeStart = new DateTime(); //at the beginning of your script
function Uptime() {
global $uptimeStart;
$end = new DateTime();
$diff = $uptimeStart->diff($end);
return $diff->format("%a days %H:%i:%s");
}
You won't get anything meaninful by calling date() on that time difference. You should take that time difference and progressively divide with years, months, days, hours, all measured in seconds. That way you'll get what the time difference in those terms.
$daySeconds = 86400 ;
$monthSeconds = 86400 * 30 ;
$yearSeconds = 86400 * 365 ;
$years = $uptime / $yearSeconds ;
$yearsRemaining = $uptime % $yearSeconds ;
$months = $yearsRemaining / $monthSeconds ;
$monthsRemaining = $yearsRemaining % $monthSeconds ;
$days = $monthsRemaining / $daySeconds ;
.. etc to get hours and minutes.
date() function with second argument set to 0 will actually return you (zero-date + (your time zone)), where "zero-date" is "00:00:00 1970-01-01". Looks like your timezone is UTC-5, so you get (365 days 24 hours) - (5 hours) = (364 days 19 hours)
Also, date() function is not the best way to show the difference between two dates. See other answers - there are are already posted good ways to calculate difference between years

Unix timestamp to seconds, minutes, hours

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

Categories