Jump to content

Recommended Posts

I have been trying to find out how to calculate in a numerical form the number of days bewteen two dates.

i tried and get an answer of 0. How can i convert the date into a number?

So basically I will have two dates, Todays date and a date stored in a database that I will retrieve Do the math and if the difference is 0-30 i do nothing, 30-34 i do one thing, 60-64 i do another another and greater than 64 i do nothing.

<code>
<?
$date1 = date("Y-m-d");
$date2 = date("Y-m-d", strtotime("+2 days"));

$date_diff = $date2 - $date1;

echo $date_diff;
?>
</code>

Thanks,

Snowdog
Link to comment
https://forums.phpfreaks.com/topic/13775-date-subtraction/
Share on other sites

The code you have now is trying to subtract strings, not numbers. The time(), mktime(), and strtotime() functions will all return the number of seconds since 1-1-1970.

Your example would then be:
[code]<?php
$date1 = time();
$date2 = strtotime("+2 days");
$date_diff = floor(($date2 - $date1)/86400);
echo $date_diff;
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/13775-date-subtraction/#findComment-53538
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.