• 0

[Javascript]Convert seconds to days hours minutes and seconds.


Question

I think I may have the solution to my other post:https://www.neowin.net/forum/index.php?showtopic=817650&view=getnewpost

I will like calculate the difference between two dates in seconds through php.

But I need someone to make a javascript function to convert a number of seconds to: Days hours:minutes:seconds

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

sigh ;-)

it's really not difficult, just some math

function secondsToString(seconds)

{

var numdays = Math.floor(seconds / 86400);

var numhours = Math.floor((seconds % 86400) / 3600);

var numminutes = Math.floor(((seconds % 86400) % 3600) / 60);

var numseconds = ((seconds % 86400) % 3600) % 60;

return numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";

}

take it from there

Link to comment
Share on other sites

  • 0

why would u wanna use js when u need php?

Try this for finding the difference in days between 2 dates/datetimes... take note though, date_parse requires PHP version 5.1.3 or higher.

<?php

/**

* Finds the difference in days between two calendar dates.

*

* @param Date $startDate

* @param Date $endDate

* @return Int

*/

function dateDiff($startDate, $endDate)

{

// Parse dates for conversion

$startArry = date_parse($startDate);

$endArry = date_parse($endDate);

// Convert dates to Julian Days

$start_date = gregoriantojd($startArry["month"], $startArry["day"], $startArry["year"]);

$end_date = gregoriantojd($endArry["month"], $endArry["day"], $endArry["year"]);

// Return difference

return round(($end_date - $start_date), 0);

}

?>

Link to comment
Share on other sites

  • 0
why would u wanna use js when u need php?

Try this for finding the difference in days between 2 dates/datetimes... take note though, date_parse requires PHP version 5.1.3 or higher.

<?php

/**

* Finds the difference in days between two calendar dates.

*

* @param Date $startDate

* @param Date $endDate

* @return Int

*/

function dateDiff($startDate, $endDate)

{

// Parse dates for conversion

$startArry = date_parse($startDate);

$endArry = date_parse($endDate);

// Convert dates to Julian Days

$start_date = gregoriantojd($startArry["month"], $startArry["day"], $startArry["year"]);

$end_date = gregoriantojd($endArry["month"], $endArry["day"], $endArry["year"]);

// Return difference

return round(($end_date - $start_date), 0);

}

?>

because he want's a countdown counter which would be stupid to do in php?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.