chrisuk Posted February 21, 2007 Share Posted February 21, 2007 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? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 21, 2007 Share Posted February 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
chrisuk Posted February 21, 2007 Author Share Posted February 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 21, 2007 Share Posted February 21, 2007 Yes, i believe you can, as long as $entrydate is in a readable format or something. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 21, 2007 Share Posted February 21, 2007 You don't need quotes around the variable name here, just use: <?php $lastday = strtotime($entrydate); ?> Ken Quote Link to comment Share on other sites More sharing options...
chrisuk Posted February 21, 2007 Author Share Posted February 21, 2007 Thanks - although it doesn't seem to be working? If I paste in the code exactly as first given (including static date entry) into a blank script, the result is just 0? Must be me doing something wrong....? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 21, 2007 Share Posted February 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
chrisuk Posted February 21, 2007 Author Share Posted February 21, 2007 This now gives the result "9912" I appreciate your assistance on this Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 21, 2007 Share Posted February 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
chrisuk Posted February 21, 2007 Author Share Posted February 21, 2007 I should slap myself...as i've just realised that the date today is the date used in the example and that is indeed why it's 0 Sorry for the confusion! Thanks very much Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 21, 2007 Share Posted February 21, 2007 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. Quote Link to comment Share on other sites More sharing options...
chrisuk Posted February 21, 2007 Author Share Posted February 21, 2007 Thanks for that, i'd kinda noticed that there was probably an issue with the way I was doing it. I've changed my scripts to use more appropriate data types What format does a datetime field expect? as I had some problems with that. Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 21, 2007 Share Posted February 21, 2007 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. Quote Link to comment Share on other sites More sharing options...
chrisuk Posted March 13, 2007 Author Share Posted March 13, 2007 as a matter of interest....why divide by 86400? thanks! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 13, 2007 Share Posted March 13, 2007 86400 = the number of seconds in a day Ken Quote Link to comment Share on other sites More sharing options...
chrisuk Posted March 14, 2007 Author Share Posted March 14, 2007 ah! thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.