Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display time until or time since a specified date javascript or php

Basically I need to display how much time till a specified time or if its passed, how much time since it passed.

The original string would look just like this: 07/20/2011 01:13 am

EDIT: switched from doing conversion in javascript to php:

//get local time in UTC format
echo gmdate("Y/m/d\TH:i:s\Z");
  echo '<br />';

 //convert time to UTC
  $the_date = strtotime("07/20/2011 01:13 am");
  echo date("Y/m/d\TH:i:s\Z",$the_date);

Doing the above gives me these to work with:

//local time
2011/07/20T19:49:39Z
//specified time
2011/07/20T01:13:00Z

How can I take the specified time, and the local time and make it display something like these examples:

Started 36 mins ago
Will start in 33 mins
Will start in 6 hrs 21 mins
Will start in 4 days 4 hrs 33 mins
like image 584
brybam Avatar asked Jul 20 '11 08:07

brybam


People also ask

How do you check date is before or after in JavaScript?

To check if a date is before another date, compare the Date objects, e.g. date1 < date2 . If the comparison returns true , then the first date is before the second, otherwise the first date is equal to or comes after the second. Copied!

How to get date and time from timestamp in JavaScript?

If you instead want to get the current time stamp, you can create a new Date object and use the getTime() method. const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.

How to fetch time from datetime in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.


1 Answers

And if you want to do it in Javascript, it is slightly more painful but you get the benefit of a "live" countdown.

Here's a great breakdown of how to do that.

http://ditio.net/2010/05/02/javascript-date-difference-calculation/

like image 193
Edgar Velasquez Lim Avatar answered Oct 22 '22 15:10

Edgar Velasquez Lim