Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Doing time difference in php

Featured Replies

Hey i was wondering how do u do time difference in php kinda like facebook does where it says something has been posted 1minute ago,13hrs or a month ago ect..

 

thanks in advance reason being im gonna use it in a project thats why i was wondering :)

calculations on the timestamp..

 

posted_time_since = time() - $posted = seconds since action

 

then its just simple maths

 

if < 60 = less than a minute

 

if > 60 = more than a minute (time / 60)

 

etc etc

  • Author

calculations on the timestamp..

 

posted_time_since = time() - $posted = seconds since action

 

then its just simple maths

 

if < 60 = less than a minute

 

if > 60 = more than a minute (time / 60)

 

etc etc

How bout days,months,ect.. :)

just expand the logic to use bigger numbers, so

 

1 day = 60*60*24 = 86400

1 week = 60*60*24*7 = 604800

1 month = 60*60*24*7*4 = 2419200

 

then if the number of seconds difference is greater than 86400 you know its over a day old and to work out how many (time_difference / 86400 = days)

  • Author

just expand the logic to use bigger numbers, so

 

1 day = 60*60*24 = 86400

1 week = 60*60*24*7 = 604800

1 month = 60*60*24*7*4 = 2419200

 

then if the number of seconds difference is greater than 86400 you know its over a day old and to work out how many (time_difference / 86400 = days)

 

Sorry about all the questions but how could i make a function and allow it to like accept a date like $row['date'] and that do the calculations?

actually, i could do with this for our website platform, seams like a thing people will like/want... give me a bit and i shall post what i make :)

  • Author

actually, i could do with this for our website platform, seams like a thing people will like/want... give me a bit and i shall post what i make :)

Thanks alot this is one thing i have struggled on lol

  • Author

Hey,

 

feeding my laziness i just just did a quick google and found http://www.9lessons.info/2010/01/php-time-stamp-function.html

 

does exactly what you need :)

 

using a slightly modded version for our platform now, thanks for the idea.

 

Np and thanks for the link and info helps alot :)

  • Author

Hey,

 

feeding my laziness i just just did a quick google and found http://www.9lessons.info/2010/01/php-time-stamp-function.html

 

does exactly what you need :)

 

using a slightly modded version for our platform now, thanks for the idea.

 

I decided to re-build the above function into a useful class and works great :) heres the code for the class

 

class convertTime{
var  $time_diff = '';
var  $secs = '';
var  $mins = '';
var  $hrs = '';
var  $days = '';
var  $weeks = '';
var  $months = '';
var  $years = '';

function timeConverter($time){
 $this->time_diff = time() - $time;
 $this->secs = $this->time_diff;
 $this->mins = round($this->time_diff/60);
 $this->hrs = round($this->time_diff/3600);
 $this->days = round($this->time_diff/86400);
 $this->weeks = round($this->time_diff/604800);
 $this->months = round($this->time_diff/2419200);

 if($this->secs <= 60){
print $this->secs. " secs ago";
}
else if($this->mins <= 60){
 if($this->mins == 1){
print "About ".$this->mins." minute ago";
}else{
print $this->mins . " minutes ago";
}
}
else if($this->hrs <= 24){
 if($this->hrs == 1){
print "One hour ago";
}
else{
print $this->hrs . " Hour's ago";
}
}
else if($this->days <= 7)
{
 if($this->days == 1)
 {
 print "About ".$this->days." day ago";
 }
 else{
  print "".$this->days." day's ago";
 }
}
else if($this->weeks <= 4){
 if($this->weeks == 1)
 {
  print "About ".$this->weeks." week ago";
 }
 else{
  print "".$this->weeks." weeks's ago";
 }
}
else if($this->months <= 12)
{
 if($this->months == 1)
 {
  print "About ".$this->months." month ago";
 }
 else{
   print "".$this->months." months's ago";
 }
}
}
}
$timeConvert = new convertTime;

 

Reason its in php 4 cause the server i have to upload my site to only has php 4 i will add a php 5 version in a few to for people who do not know how to convert it over to php5.

 

to use the class just include the file and use it like this

 

$timeConvert->timeConverter($row['time']);

 

You must have done a query before that though cause the example assumes ur pulling a timestamp from mysql, only excepts timestamps enjoy :)

Reason its in php 4 cause the server i have to upload my site to only has php 4 i will add a php 5 version in a few to for people who do not know how to convert it over to php5.

 

If you're going to make a PHP 5 object for this, consider extending the DateInterval & DateTime SPL objects that come with PHP 5.3

 

http://www.php.net/manual/en/class.dateinterval.php

 

I'd be interested in a copy if you make and GPL it.

  • Author

If you're going to make a PHP 5 object for this, consider extending the DateInterval & DateTime SPL objects that come with PHP 5.3

 

http://www.php.net/manual/en/class.dateinterval.php

 

I'd be interested in a copy if you make and GPL it.

 

Can u ellaberate a little bit more on what dateinterval does and what its for the link u gave me does not really say to much things about it thanks in advance :)

  • Author

Ok i got a php 5 version done now of that class imma attach the file to this post, the php 4 version is also included inside the file, the class will check if u have php 5 or 4 then run the appropriate class for whichever version of php u have :)

timeConvert.class.php

Ok i got a php 5 version done now of that class imma attach the file to this post, the php 4 version is also included inside the file, the class will check if u have php 5 or 4 then run the appropriate class for whichever version of php u have :)

 

Thanks steve, This is full working :)

Hello,

 

Ive taken steves above and re-written it as my own, but with years and cleaned up the code.

 

<?php
//********************************//
// CREATOR: SHAUN CHILDERLEY
// FILE VERSION: 1.0.0
// FILE NAME: TIME.CLASS.PHP
// COMMENTS: Time Difference Handler
//********************************//
if(!defined('YOUR_CONSTANT')) die();
class time_diff {

private $time_diff;
private $seconds;
private $minutes;
private $hours;
private $days;
private $weeks;
private $months;
private $years;

public function run($time) {

$this->time_diff = time() - $time;
$this->seconds = $this->time_diff;
$this->minutes = round($this->time_diff/60);
$this->hours = round($this->time_diff/3600);
$this->days = round($this->time_diff/86400);
$this->weeks = round($this->time_diff/86400*7);
$this->months = round($this->time_diff/86400*30);
$this->years = round($this->time_diff/86400*30*12);

if($this->seconds < 60) { if($this->seconds == 1) {echo 'About '.$this->seconds.'second ago';} else { echo 'About '.$this->seconds.' seconds ago';}
} elseif($this->minutes < 60) { if($this->minutes == 1) {echo 'About '.$this->minutes.' minute ago';} else { echo 'About '.$this->minutes.' minutes ago';}
} elseif($this->hours < 24) { if($this->hours == 1) {echo 'About '.$this->hours.' hour ago'; } else {echo 'About '.$this->hours.' hours ago';}
} elseif($this->days < 7) { if($this->days == 1) { echo 'About '.$this->days.' day ago';} else { echo 'About '.$this-days.' days ago';}
} elseif($this->weeks < 4) { if($this->weeks == 1) { echo 'About '.$this->weeks.' week ago'; } else {echo 'About '.$this->weeks.' weeks ago';}
} elseif($this->months < 12) { if($this->months == 1) { echo 'About '.$this->months.' month ago'; } else { echo 'About '.$this->months.' months ago';}
} else{if($this->years == 1) { echo 'About '.$this->years.' year ago';} else { echo 'About '.$this->years.' years ago';}}

}
}

$time_diff=new time_diff();
?>

 

- Shaun

  • Author

Hello,

 

Ive taken steves above and re-written it as my own, but with years and cleaned up the code.

 

<?php
//********************************//
// CREATOR: SHAUN CHILDERLEY
// FILE VERSION: 1.0.0
// FILE NAME: TIME.CLASS.PHP
// COMMENTS: Time Difference Handler
//********************************//
if(!defined('YOUR_CONSTANT')) die();
class time_diff {

private $time_diff;
private $seconds;
private $minutes;
private $hours;
private $days;
private $weeks;
private $months;
private $years;

public function run($time) {

$this->time_diff = time() - $time;
$this->seconds = $this->time_diff;
$this->minutes = round($this->time_diff/60);
$this->hours = round($this->time_diff/3600);
$this->days = round($this->time_diff/86400);
$this->weeks = round($this->time_diff/86400*7);
$this->months = round($this->time_diff/86400*30);
$this->years = round($this->time_diff/86400*30*12);

if($this->seconds < 60) { if($this->seconds == 1) {echo 'About '.$this->seconds.'second ago';} else { echo 'About '.$this->seconds.' seconds ago';}
} elseif($this->minutes < 60) { if($this->minutes == 1) {echo 'About '.$this->minutes.' minute ago';} else { echo 'About '.$this->minutes.' minutes ago';}
} elseif($this->hours < 24) { if($this->hours == 1) {echo 'About '.$this->hours.' hour ago'; } else {echo 'About '.$this->hours.' hours ago';}
} elseif($this->days < 7) { if($this->days == 1) { echo 'About '.$this->days.' day ago';} else { echo 'About '.$this-days.' days ago';}
} elseif($this->weeks < 4) { if($this->weeks == 1) { echo 'About '.$this->weeks.' week ago'; } else {echo 'About '.$this->weeks.' weeks ago';}
} elseif($this->months < 12) { if($this->months == 1) { echo 'About '.$this->months.' month ago'; } else { echo 'About '.$this->months.' months ago';}
} else{if($this->years == 1) { echo 'About '.$this->years.' year ago';} else { echo 'About '.$this->years.' years ago';}}

}
}

$time_diff=new time_diff();
?>

 

- Shaun

 

Just one thing i wanna point out this line right here if(!defined('YOUR_CONSTANT')) die(); make sure people understand they need to define a constent above the included file like DEFINE("YOUR_CONSTANT",1); other then that looks great :) when i get the chance i may rewrite mine to lol, i'd also like to point one more thing u prob should be using else if considering theres no need to check for all of them at once :)

Just one thing i wanna point out this line right here if(!defined('YOUR_CONSTANT')) die(); make sure people understand they need to define a constent above the included file like DEFINE("YOUR_CONSTANT",1); other then that looks great :) when i get the chance i may rewrite mine to lol, i'd also like to point one more thing u prob should be using else if considering theres no need to check for all of them at once :)

 

Thanks mate, Will Improve :) +1

  • Author

btw i just noticed something this line right here

 

$this->years = round($this->time_diff/86400*30*12);

u would not times 86400 by 30 it would be

86400 *7*4*12

basically thats saying the number of miliseconds in a day times 7 days which is a week times 4 which is the number of weeks in a month times 12 which is the number of months in a year :) which gives u 29030400 the number of miliseconds in a year

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.