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
https://forums.phpfreaks.com/topic/39459-solved-simple-date-calculations/
Share on other sites

What you should do is convert them to strtotime form before the calculations:

$today = strtotime("now");
$lastday = strtotime("2007-02-21 11:22am");
$timediff = $today - $lastday;
$dateform = $timediff/86400;
$dateform = round($dateform);
echo $dateform;

Ted

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 :)

 

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

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

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.

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.

  • 3 weeks later...

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.