gladiator83x Posted June 17, 2006 Share Posted June 17, 2006 Hey All, I tried asking this question yesterday, but the feedback that I received did not really answer my question. I just wanted to know if there was a way to take the difference between two Unix time stamps in php; with both of them in the form of: 2006-06-17 09:50:37? Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/ Share on other sites More sharing options...
poirot Posted June 17, 2006 Share Posted June 17, 2006 You can use MySQL's DATEDIFF function:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]DATEDIFF(expr1,expr2) DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation. [/quote][a href=\"http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/dat...-functions.html[/a]As for doing that with PHP, check the user notes here:[a href=\"http://www.php.net/mktime\" target=\"_blank\"]http://www.php.net/mktime[/a] Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/#findComment-46664 Share on other sites More sharing options...
gladiator83x Posted June 18, 2006 Author Share Posted June 18, 2006 [!--quoteo(post=384976:date=Jun 17 2006, 10:33 AM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 17 2006, 10:33 AM) [snapback]384976[/snapback][/div][div class=\'quotemain\'][!--quotec--]You can use MySQL's DATEDIFF function:[a href=\"http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/dat...-functions.html[/a]As for doing that with PHP, check the user notes here:[a href=\"http://www.php.net/mktime\" target=\"_blank\"]http://www.php.net/mktime[/a][/quote]I am having a problem using the DATEDIFF function. This is what I am typing in:$diff= SELECT DATEDIFF($test_date,$creation_ts);echo $diff;This is the output that I receive:SELECT DATEDIFF(2006-06-17 21:24:48,2004-08-09 11:01:24)It doesn't subtract the dates. When I typed in the same thing minus the SELECT, it didnt recognize the DATEDIFF function.$diff= DATEDIFF($test_date,$creation_ts);echo $diff;Anyone have any suggestions??? Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/#findComment-46875 Share on other sites More sharing options...
Fyorl Posted June 18, 2006 Share Posted June 18, 2006 You need to actually use MySQL, no use typing SQL commands into PHP variable if you're not going to query it. [code]$sql = mysql_query("SELECTDATEDIFF('$test_date', '$creation_ts')");$diff = mysql_result($sql, 0);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/#findComment-46878 Share on other sites More sharing options...
robos99 Posted June 18, 2006 Share Posted June 18, 2006 Maybe I'm nitpicking here but isn't 2006-06-17 09:50:37 not a Unix timestamp? I may be wrong though. Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/#findComment-46891 Share on other sites More sharing options...
Fyorl Posted June 18, 2006 Share Posted June 18, 2006 [!--quoteo(post=385208:date=Jun 17 2006, 09:33 PM:name=robos99)--][div class=\'quotetop\']QUOTE(robos99 @ Jun 17 2006, 09:33 PM) [snapback]385208[/snapback][/div][div class=\'quotemain\'][!--quotec--]Maybe I'm nitpicking here but isn't 2006-06-17 09:50:37 not a Unix timestamp? I may be wrong though.[/quote]I'm pretty sure that's a Unix timestamp, check the PHP documentation Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/#findComment-46899 Share on other sites More sharing options...
gladiator83x Posted June 18, 2006 Author Share Posted June 18, 2006 [!--quoteo(post=385208:date=Jun 17 2006, 10:33 PM:name=robos99)--][div class=\'quotetop\']QUOTE(robos99 @ Jun 17 2006, 10:33 PM) [snapback]385208[/snapback][/div][div class=\'quotemain\'][!--quotec--]Maybe I'm nitpicking here but isn't 2006-06-17 09:50:37 not a Unix timestamp? I may be wrong though.[/quote]Hi Robos99,I believe that it is definitely a Unix Timestamp. Check out the link:[a href=\"http://en.wikipedia.org/wiki/Unix_epoch\" target=\"_blank\"]http://en.wikipedia.org/wiki/Unix_epoch[/a] Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/#findComment-46900 Share on other sites More sharing options...
gladiator83x Posted June 18, 2006 Author Share Posted June 18, 2006 [!--quoteo(post=385217:date=Jun 17 2006, 10:49 PM:name=gladiator83x)--][div class=\'quotetop\']QUOTE(gladiator83x @ Jun 17 2006, 10:49 PM) [snapback]385217[/snapback][/div][div class=\'quotemain\'][!--quotec--]Hi Robos99,I believe that it is definitely a Unix Timestamp. Check out the link:[a href=\"http://en.wikipedia.org/wiki/Unix_epoch\" target=\"_blank\"]http://en.wikipedia.org/wiki/Unix_epoch[/a][/quote]The code that Fyorl definitely looks correct, and I know that I am connecting to the database but I keep getting the following error:Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /export/home2/webpages/confirm.php on line 86I changed it up a couple of times, and still nothing. Anybody else ever get this error message, and if so...what did you do to fix it? Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/#findComment-46917 Share on other sites More sharing options...
poirot Posted June 18, 2006 Share Posted June 18, 2006 You are not picking it up guys... Usually, a UNIX timestamp is an integer representing the seconds since the UNIX epoch.1150552200 = 06/17/2006 - 09:50Now, MySQL doesn't need a UNIX timestamp to calculate DATEDIFF.You are going to need MySQL >= 4.1 to use it though. Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/#findComment-46920 Share on other sites More sharing options...
jajtiii Posted June 18, 2006 Share Posted June 18, 2006 A Unix Timestamp is [b]always[/b] an integer. Month names, day names and the roman values of years have nothing to do with a Unix Timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/#findComment-46949 Share on other sites More sharing options...
poirot Posted June 18, 2006 Share Posted June 18, 2006 [!--quoteo(post=385266:date=Jun 18 2006, 03:07 AM:name=jajtiii)--][div class=\'quotetop\']QUOTE(jajtiii @ Jun 18 2006, 03:07 AM) [snapback]385266[/snapback][/div][div class=\'quotemain\'][!--quotec--]A Unix Timestamp is [b]always[/b] an integer. Month names, day names and the roman values of years have nothing to do with a Unix Timestamp.[/quote]Thanks for your remark jajtii. I didn't want to use a forceful word because my definition of "timestamp" leaves a gray area for interpretation, what I mean with "usually" is that 'Jun 18 2006, 03:07 AM' for example is a "Unix [compatible] timestamp" [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /] If this is not correct, please tell me Quote Link to comment https://forums.phpfreaks.com/topic/12237-unix-timestamp-problem/#findComment-46968 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.