Jump to content

[SOLVED] Get Number Of Days


Daney11

Recommended Posts

This should work.

 

<?php
   $date1 = strtotime( "now" );
   $date2 = strtotime( "2010-02-20 22:58:00" );
   $dateDiff = $date2 - $date1;
   $daysRemaining = floor($dateDiff/(60*60*24));
   echo $daysRemaining . " day(s)";
?>

Link to comment
Share on other sites

Thanks again for your help.

 

   

   $date1 = strtotime(2009-03-12 12:27:00);
   $date2 = strtotime(2009-04-12 22:58:00);
   $dateDiff = $date1 - $date2;
   $daysRemaining = floor($dateDiff/(60*60*24));

 

Works, however im getting "-32 Days Remaining"

 

Im not sure where the "-" is coming from.

 

Edited, seen your edited version.

 

Thanks a lot

Link to comment
Share on other sites

Hi

 

That is partly because of the order of the dates. Just put the most recent of the 2 date first when subtracting one from the other.

 

The other issue is that is usng the full date / times (which might well be what you want). So if you compared 23:59 on one day with 00:01 on the next day it is returning 120 second then dividing that by the number of seconds in a day and using floor which would return 0. In such a situation you might want to still return 1 day, in which case change it to:

 

   $date1 = strtotime(2009-04-12 22:58:00);
   $date2 = strtotime(2009-03-12 12:27:00);
   $daysRemaining = floor($date1/(60*60*24)) - floor($date2/(60*60*24));
[/code

(or rather than dividing by 60*68*24 you could just chop the time part off the date before passing it to strtime).

All the best

Keith

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.