Jump to content

[SOLVED] simple date calculations


chrisuk

Recommended Posts

I'm a bit stumped with dates.

 

What I am trying to do is show the difference between two dates - one will be "today" and the other will be the date on which an entry was made into the database for that particular user.

 

I tried doing $difference = $today - $entrydate but this did not give me any kind of figure.

 

Both dates are stored in the format of, for example, 21 Feb 07, 11:22am

 

Would the time cause problems?

 

 

Link to comment
Share on other sites

Ted, that looks just like what I need. I had a feeling that strtotime would come into it...

 

Before I go ahead and mark this as solved - the line:

 

$lastday = strtotime("2007-02-21 11:22am");

 

Could this be replaced with:

 

$lastday = strtotime("$entrydate");

 

As they are all different you see :)

 

Link to comment
Share on other sites

Try inserting in something to check your code up to that step:

$today = strtotime("now");
$lastday = strtotime("2007-02-21 11:22am");
$timediff = $today - $lastday;
echo $timediff;//this is what I have added, check if it outputs some large number.
echo round($timediff/86400);

Ted

Link to comment
Share on other sites

oh right, yeah, since the result is 9912 and the code divides that by 86400, and round it up, the result is 0! Which is the number of days between the two times. and I believe the outcome is expected.

btw, dont you want the no. of days between the two times? or do you want something else? because that was my interpretation of your first post.

Ted

Link to comment
Share on other sites

Both dates are stored in the format of, for example, 21 Feb 07, 11:22am

 

Would the time cause problems?

 

Not good practice, since you're going to have to do a lot of additional parsing to get the calculations to come out right. If you will get into the habit of storing dates in a MySQL DATETIME field (using the datatypes for what they are intended), you will be able to rely on MySQL to do your calculations for you with a couple function calls, and you won't even have to worry with the PHP side of things.

Link to comment
Share on other sites

What format does a datetime field expect? as I had some problems with that.

 

The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

 

You can find a lot more information on how to initiate and actually take advantage of using date and time data on this page of the MySQL manual.

 

Good luck.

Link to comment
Share on other sites

  • 3 weeks later...
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.