Time Ago Using Php

Discussion in 'Web Design' started by electrode, Jun 26, 2016.

  1. electrode

    electrode Well Known Mod

    You might have been wondering how online social communities parse php raw date/time into a neat user readable term ( i dont know the right word to use here hehe).
    you can easily change or parse php raw date and time using the simple php function below

    timeAgo($time_ago) { $cur_time time(); $time_elapsed $cur_time $time_ago$seconds $time_elapsed $minutes round($time_elapsed 60 ); $hours round($time_elapsed 3600); $days round($time_elapsed 86400 ); $weeks round($time_elapsed 604800); $months round($time_elapsed 2600640 ); $years round($time_elapsed 31207680 ); // Seconds if($seconds <= 60){ return "just now"; } //Minutes else if($minutes <=60){ if($minutes==1){ return "one minute ago"; } else{ return "$minutes minutes ago"; } } //Hours else if($hours <=24){ if($hours==1){ return "an hour ago"; }else{ return "$hours hrs ago"; } } //Days else if($days <= 7){ if($days==1){ return "yesterday"; }else{ return "$days days ago"; } } //Weeks else if($weeks <= 4.3){ if($weeks==1){ return "a week ago"; }else{ return "$weeks weeks ago"; } } //Months else if($months <=12){ if($months==1){ return "a month ago"; }else{ return "$months months ago"; } } //Years else{ if($years==1){ return "one year ago"; }else{ return "$years years ago"; } } } 
    example below

    echo "<h2>See examples for random timestamps</h2>"$timestamp rand(100000000,1000000000); echo "<b>".$timestamp."</b>: " timeAgo($timestamp) . "<br>"$timestamp time()-rand(1000000,10000000); echo "<b>".$timestamp."</b>: " timeAgo($timestamp) . "<br>"$timestamp time()-rand(10000,1000000); echo "<b>".$timestamp."</b>: " timeAgo($timestamp) . "<br>";
    In case you have any issue with this function let me know.
  2. Loading...

    Similar Threads - Ago Using Php Forum Date
    How To Create A Login And Registration System Using Php Web Design Jul 17, 2016

  3. Prezzy

    Prezzy Leader Protector

    Feeling you bro! Keep it up but we are yet to discuss and give this a structure.
  4. bibest

    bibest Active

    Just like magic dnt know about php but will like to know it
  5. electrode

    electrode Well Known Mod

    Stay tuned bro soon you will
  6. bibest

    bibest Active

    thanks that will be great
  7. Prezzy

    Prezzy Leader Protector

    Bro, that script is an advanced one cos it constitutes of so many things in one

    Mathematical operations
    If statements
    Time functions

    Am guessing you aren't yet teaching right cos that's not a cool way to start considering you'll be dealing with kiddies in php