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-

 

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.