Jump to content

Counting Number of Day's until a specific date


leachus2002

Recommended Posts

Easy, you can even do seconds, in fact its easier to do seconds with timestamps:

<?php

// The time your going to check against
$date1 = "25/12/2010"; // Christmas!

// Current timestamp (Current Date/Time)
$current = time();

// Convert that date string above into a timestamp as well.
$until = strtotime($date1);

// Get the difference in seconds between two dates. (Very simple math)
$difference = $until - $current;

// Get number of days (Also very simple math)
$days = (($difference / 60) / 60) / 24; // Seconds / minutes / hours / days

// Results
echo("Number of days until christmas: ".$days." - Number of Seconds: ".$difference."<br />");

?>

 

Hope this helps,

-cb-

 

Oops, strtotime doesnt understand " / ", change the " / " in the date to " - ", eg: 25-12-2010

 

Also you can use round() to get a less specific long number. or floor() to remove the decimal entirely.

 

-cb-

 

NOTE: As pikachu said you could use the DateTime Object (if you have PHP 5.3 or greater). - To get your version use phpinfo();

Oops, strtotime doesnt understand " / ", change the " / " in the date to " - ", eg: 25-12-2010

 

Also you can use round() to get a less specific long number. or floor() to remove the decimal entirely.

 

-cb-

 

NOTE: As pikachu said you could use the DateTime Object (if you have PHP 5.3 or greater). - To get your version use phpinfo();

 

strtotime() treats dates with / as mm/dd/yyyy and dates with - as dd-mm-yyyy or yyyy-mm-dd.

Hello,

 

Here is a very simple way to get this done

 

// Define final day -- if this is less than the $startdate, then I am not sure what will happen  ^.^
$enddate = "2010-12-25";

// Calculation of difference between now, and the end date
$startdate = time();
$enddate = strtotime($enddate);
$diff = $enddate - $startdate;
$days = ceil($diff / 86400);

// Output total
echo $days;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.