Code solutions to PHP, JS, CSSCode source PHP, JS, CSS

Spread your code soluces to the world

Love me
Solucior » PHP » Display natural human dates for past dates

Display natural human dates for past dates

09/17/2014 by Dux   -   not rated

Little function to display past dates with a natural human language as on iphone based messages.
It can write dates :
now, 5mn ago, today at 12:18, yesterday at 08:20, monday at 21:12, 3 november at 12:18 (modify function for english users), 24 december 2013 at 23:59

source code



 

// natural dates
function natdate($date) {

       
$jour = array("", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
       
$mois = array("", "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");
       
       
// Inf a 1mn
       
if (time()-$date < 60) return ("now");
       
// Inf a 1H
       
if (time()-$date < 60*60) return (round((time()-$date)/60)."mn ago" );
       
// Si aujourd'hui
       
if (date("Y", $date) == date("Y") && date("z", $date) == date("z")) return ("today at ".date("H:i", $date));
       
// Si hier
       
if (time() - $date < 60*60*48) return ("yesterday at ".date("H:i", $date));
       
// Si moins d'une semaine
       
if (time() - $date < 60*60*24*7) return ($jour[date("N", $date)]." at ".date("H:i", $date));
       
// Si cette année
       
if (time() - $date < 60*60*24*365) return(date("j", $date)." ".$mois[date("n", $date)]. " at ".date("H:i", $date));
       return(
date("j", $date)." ".$mois[date("n", $date)]." ".date("Y H:i", $date));
               
}
Rate this code     |     Copied : 0x

Comments for Display natural human dates for past dates

No comment
Add a comment Nickname
ZOOM


Display a date in French
Display natural human dates for past dates
Force file download
PHP watermark
Infinite zoom CSS3 and Javascript
Dynamically add rows to a form. Complete solution
Zoom with CSS3
MySQL table statistics
Simple method : listing files from a directory