Hey guys, I want to compare the time difference, or hours really (as in hour count) between two dates. I store the dates in mysql in a separate date/time row which has the following format: 2009-10-29 13:00:00 And, what I want to do is calculate and print how many hours there are between, let's say that date, and current time. Have absolutely no idea how to do this but hopefully it's not too hard.
Use mktime to convert your time from the database ( string ) to the UNIX timestamp and compare it with time ( returns the current time - UNIX timestamp format ).
Right - just to further explain, here's a bit of code. I personally use strtotime, which converts time into a UNIX datestamp too, just with a bit more flexibility, (UNIX datestamp being the number of seconds since January 1 1970 00:00:00 UTC, makes sense to geeks, don't worry!) and then it's easy to do stuff: $firstTime = [your first time here]; $timeNow = strtotime('now'); // get current time - it knows what "now" is, or you can do "next Friday" and stuff like that, that's why it's more flexible than mktime $timeDiff = ($timeNow - $firstTime) / 3600; // divide by 3600 to get hours Code (markup): Easy-peasy, though dates and times can seem scary, and I think they used to be, now they can easily be handled in UNIX format and then converted back to whatever format you want using the php date() function. I used this code to do exactly what you want in a script of mine that only runs every 24 hours, but is called every ten minutes and checks to see if 24 hours have passed yet since the last run.
This is so easy. You have to create PHP function for that and pass the datetime parameters to the function and get the time difference in hours. <?php function timeDiff($firstTime,$lastTime) { // convert to unix timestamps $firstTime=strtotime($firstTime); $lastTime=strtotime($lastTime); // perform subtraction to get the difference (in seconds) between times $timeDiff=$lastTime-$firstTime; // return the difference return $timeDiff; } //Usage : echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32"); ?>
hiii, you can do this by extracting the string like. month, date, year, from the value that is entered in the database. After that you can we able to compare to date. or you can do this by using this method. echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32"); Try this. If it don't work give me a reply message. i will send you all the coding about how to do that.