Jump to content


Photo

Unix Timestamp Problem


  • Please log in to reply
10 replies to this topic

#1 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 17 June 2006 - 01:57 PM

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?

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 17 June 2006 - 02:33 PM

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]
~ D Kuang

#3 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 18 June 2006 - 01:45 AM

[!--quoteo(post=384976:date=Jun 17 2006, 10:33 AM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 17 2006, 10:33 AM) View Post[/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???


#4 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 18 June 2006 - 01:58 AM

You need to actually use MySQL, no use typing SQL commands into PHP variable if you're not going to query it.
$sql = mysql_query("
SELECT
DATEDIFF('$test_date', '$creation_ts')
");
$diff = mysql_result($sql, 0);

[table]



Don't worry, the printer fairies will sort it out.

#5 robos99

robos99
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 18 June 2006 - 02:33 AM

Maybe I'm nitpicking here but isn't 2006-06-17 09:50:37 not a Unix timestamp? I may be wrong though.

#6 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 18 June 2006 - 02:49 AM

[!--quoteo(post=385208:date=Jun 17 2006, 09:33 PM:name=robos99)--][div class=\'quotetop\']QUOTE(robos99 @ Jun 17 2006, 09:33 PM) View Post[/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
[table]



Don't worry, the printer fairies will sort it out.

#7 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 18 June 2006 - 02:49 AM

[!--quoteo(post=385208:date=Jun 17 2006, 10:33 PM:name=robos99)--][div class=\'quotetop\']QUOTE(robos99 @ Jun 17 2006, 10:33 PM) View Post[/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]



#8 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 18 June 2006 - 03:39 AM

[!--quoteo(post=385217:date=Jun 17 2006, 10:49 PM:name=gladiator83x)--][div class=\'quotetop\']QUOTE(gladiator83x @ Jun 17 2006, 10:49 PM) View Post[/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 86

I 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?



#9 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 18 June 2006 - 04:19 AM

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

Now, MySQL doesn't need a UNIX timestamp to calculate DATEDIFF.
You are going to need MySQL >= 4.1 to use it though.
~ D Kuang

#10 jajtiii

jajtiii
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 18 June 2006 - 10:07 AM

A Unix Timestamp is always an integer. Month names, day names and the roman values of years have nothing to do with a Unix Timestamp.

#11 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 18 June 2006 - 02:15 PM

[!--quoteo(post=385266:date=Jun 18 2006, 03:07 AM:name=jajtiii)--][div class=\'quotetop\']QUOTE(jajtiii @ Jun 18 2006, 03:07 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
A Unix Timestamp is always 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
~ D Kuang




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users