Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date to timestamp

Status
Not open for further replies.

nemini

Programmer
Sep 22, 2008
21
0
0
US
if I apply date() to my timestamp example it echoes 2007,10, 09
in order to verify that the reverse is correct
I try generating a timestamp from
(0,0,0,10,9,2007)
but the result is not the expected one
how could I make a timestamp from a date using the above value?
I may not figure what I am missing in the following code
Code:
<?php

//EXAMPLE
$regTime=1191955182;
$reg_time= date("Y-m-d",$regTime);
echo"reg_time: $reg_time<P>"; // echo 2007-10-09 


// B) reverse date to timestamp
// and the returned result is not correct
// 1191902400 instead of expected 1191955182

 
$timestamp= mktime(0,0,0,10,9,2007); // note no leading zero m d y
echo"mktime: $timestamp<P>";
?>
 
The values you are using for the mktime function don't have hours, minutes or seconds as evidenced by your zeroes, however the timestamp you are using definitely is not an exact date like you are assuming it to be so it has hours, minutes and seconds to it.

That would be what's throwing of your result.

In other words your zeroes should not be zeroes.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks, I did not figure that out
so I tested with
$reg_time= date("G-i-s-m-d-Y",$regTime);
and ketp the zeros in
$timestamp= mktime(0,0,0,10,9,2007);
Next
added G-i-s value to $timestamp
The final result is now as expected.
However it sounds quite a long procedure to reach the desired result.
Any way to shorten the process?
Regards.
 
As I said your Zeroes should not be zeroes.
Code:
$reg_time= date("Y-m-d G:i:s",$regTime);

then:

$timestamp= mktime(13,39,42,10,9,2007);

As for a quicker way to get a date into timestamp format:

You can try strtotime()

but you need to include minutes, hours and seconds to maintain accuracy.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top