I want to display x seconds ago
based on a MySQL Timestamp.
I found the plugin called timeago
But I cannot find a way to make it display only seconds.
I'm OK with 61 seconds, or 300 seconds. Any way to convert the output to seconds with timeago, or with pure jQuery / JavaScript ?
Thanks for any help !
Edit:
Sample Case :
$time1 = "2014-02-02 23:54:04"; // plus PHP ways to format it.
//$time2 = NOW() , or date(); whatever.
I just want to get how many seconds since $time1
.
Edit 2:
Working code :
<script type="text/javascript">
$(function(){
var d2 = new Date();
var d1 = new Date("2014-02-02 23:54:04");
$("a#timedif").html("Diff. Seconds : "+((d2-d1)/100).toString());
// note that you may want to round the value
});
</script>
It outputs Diff. Seconds : NaN
If you are okay with that plugin you can modify it a little bit to work with seconds only
with this part, go only with converting to seconds
see the fiddle: http://jsfiddle.net/zGXLU/1/
My working with time difference for calculating each value separately
jQuery is just a JavaScript library so JavaScript will just work within your jQuery script:
assuming you parse the string into JavaScript date object, you can do (date2 - date1)/1000
to parse mysql format timestamp, just feed the string into new Date():
fix of edit 2 in the question: