php timestamp and javascript timestamp function
May-24-2014 in Ajax, Javascript, jQuery, PHP No comments
PHP Timestamp Function
The following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
?php function time_stamp($session_time) { $time_difference = time() - $session_time ; $seconds = $time_difference ; $minutes = round($time_difference / 60 ); $hours = round($time_difference / 3600 ); $days = round($time_difference / 86400 ); $weeks = round($time_difference / 604800 ); $months = round($time_difference / 2419200 ); $years = round($time_difference / 29030400 ); // Seconds if($seconds <= 60) { echo "$seconds seconds ago"; } //Minutes else if($minutes <=60) { if($minutes==1) { echo "one minute ago"; } else { echo "$minutes minutes ago"; } } //Hours else if($hours <=24) { if($hours==1) { echo "one hour ago"; } else { echo "$hours hours ago"; } } //Days else if($days <= 7) { if($days==1) { echo "one day ago"; } else { echo "$days days ago"; } } //Weeks else if($weeks <= 4) { if($weeks==1) { echo "one week ago"; } else { echo "$weeks weeks ago"; } } //Months else if($months <=12) { if($months==1) { echo "one month ago"; } else { echo "$months months ago"; } } //Years else { if($years==1) { echo "one year ago"; } else { echo "$years years ago"; } } } ?> |
How to use this php timestamp function?
Then simply include the ‘$timeago’ reference within your (what is presumably) repeated time ago output.
1 2 3 4 5 6 |
<?php $session_time=time(); // Ex: $session_time ="1264326122"; //$timeago = time_stamp($session_time); echo $timeago; ?> |
Javascript Timestamp Function Or Time ago Function
The following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<script type="text/javascript"> function timestamp_timeAgo(time){ var units = [ { name: "second", limit: 60, in_seconds: 1 }, { name: "minute", limit: 3600, in_seconds: 60 }, { name: "hour", limit: 86400, in_seconds: 3600 }, { name: "day", limit: 604800, in_seconds: 86400 }, { name: "week", limit: 2629743, in_seconds: 604800 }, { name: "month", limit: 31556926, in_seconds: 2629743 }, { name: "year", limit: null, in_seconds: 31556926 } ]; var diff = (new Date() - new Date(time*1000)) / 1000; if (diff < 1) return "just now"; var i = 0; while (unit = units[i++]) { if (diff < unit.limit || !unit.limit){ var diff = Math.floor(diff / unit.in_seconds); return diff + " " + unit.name + (diff>1 ? "s ago" : " ago"); } }; } </script> |
How to use this javascript timestamp function?
You have to use the timestamp_timeAgo(time) function to display the date and time.
1 2 3 4 5 6 7 8 9 10 11 12 |
<script type="text/javascript"> $session_time=time(); // Ex: $session_time ="1264326122"; $(document).ready(function () { var refresh_time = setInterval(function() { $(#time).html(timestamp_timeAgo()) }, 1000); $.ajaxSetup({ cache: false }); }); </script> |