  Johnny Premium join:2001-06-27 Atlanta, GA
·Comcast
| date string to Unix_Timestamp syntax
Hey,
I wound up with a date-time string in mySQL column, and I've been trying to just convert it to an integer Unix timestamp. I tried several of the PHP functions but none did a a simple conversion.
So
1. Is there in fact a PHP function that will just take another PHP variable (say $datetime that has its value "2009-10-31 12:40:50") and return an integer that is the Unix_Timestamp?
2. Failing that, I tried to make a calculated column in my MySQL DB by the following:
$query = "INSERT INTO Feedbacks (datetimestring, unixtimestamp) VALUES ('$datetimestring', 'UNIX_TIMESTAMP('$datetimestring')')";
Can somebody clue me in on how to get Unix timestamp from this string? I'd prefer to just calculate it in PHP rather than store it in the database, but either will do.
NOTE: I'm sure people are curious why I convert to string and then back to a timestamp. It's because the original date came in big-endian. |
|
  ekiM Oh Well
join:2001-01-06 /usr/home clubs: | How about strtotime() ? |
|
  twizlar I dont think so. Premium join:2003-12-24 Brantford, ON
| reply to Johnny I would probably just use UNIX_TIMESTAMP when you are pulling the data from mysql.
-- Broadline Networks Inc. |
|
  Johnny Premium join:2001-06-27 Atlanta, GA
·Comcast
| reply to ekiM said by ekiM :How about strtotime() ? I tried that, but it says it returns it relative to either a supplied timestamp or to the current time.
The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now , or the current time if now is not supplied.
So that was confusing, since I don't want it relative to anything - just a simple conversion to integer so I can compare it to another timestamp in another table. |
|
  GILXA1226 Premium,MVM join:2000-12-29 London, OH clubs:
| said by Johnny :said by ekiM :How about strtotime() ? I tried that, but it says it returns it relative to either a supplied timestamp or to the current time. The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now , or the current time if now is not supplied.
So that was confusing, since I don't want it relative to anything - just a simple conversion to integer so I can compare it to another timestamp in another table. Why not just pass it January 1 1970 for the relative date? -- We don't give a d@mn for the whole state of Michigan... we're from OHIO! O!H! ... I!O! |
|
  Johnny Premium join:2001-06-27 Atlanta, GA
·Comcast
| I'll give that a shot - seems like a hack though.
I kludged another way, converting the 4 bytes of the network-order stream I receive into a hex string and then back to decimal. Somehow in that process, the endianness gets set to little-endian.
Now it's just a matter of which is the better hack. |
|
  GILXA1226 Premium,MVM join:2000-12-29 London, OH clubs:
| said by Johnny :I'll give that a shot - seems like a hack though. I kludged another way, converting the 4 bytes of the network-order stream I receive into a hex string and then back to decimal. Somehow in that process, the endianness gets set to little-endian. Now it's just a matter of which is the better hack. What type of system is the data coming from and going to? Those conversions should not be changing the endian ness of the data unless something is being converted in a strange way. -- We don't give a d@mn for the whole state of Michigan... we're from OHIO! O!H! ... I!O! |
|
  Johnny Premium join:2001-06-27 Atlanta, GA
·Comcast
| said by GILXA1226 :said by Johnny :I'll give that a shot - seems like a hack though. I kludged another way, converting the 4 bytes of the network-order stream I receive into a hex string and then back to decimal. Somehow in that process, the endianness gets set to little-endian. Now it's just a matter of which is the better hack. What type of system is the data coming from and going to? Those conversions should not be changing the endian ness of the data unless something is being converted in a strange way. Well this is the big question. I do an fread() in a loop to get 38 bytes of binary data at a time until there is no more. Apple says (see below) that the first 4 bytes are a Unix timestamp in "network order" (big endian). Intel servers, like my Linux/PHP/MySQL/Apache server on which I am running this script, use little-endian order. In fact, all of the Objective-C utilities which read this stream reverse the byte order as they receive it. What I could not find out is whether PHP knows to do this or not.
Each of these 38-byte items is defined by Apple as:
Begin reading the stream written by the feedback service until there is no more data to read. The received data is in tuples having the following format: Figure 4-2 Binary format of a feedback tuple

time_t A timestamp (as a four-byte time_t value) indicating when the APNs determined that the application no longer exists on the device. This value, which is in network order, represents the seconds since 1970, anchored to UTC. You should use the timestamp to determine if the application on the device re-registered with your service since the moment the device token was recorded on the feedback service. If it hasnt, you should cease sending push notifications to the device.
token length The length of the device token as a two-byte integer value in network order.
device token The device token in binary format.
|
|
  cowboyro
join:2000-10-11 Shelton, CT
·AT&T U-Verse
·Comcast
·Optimum Voice
1 edit | reply to Johnny strtotime()
|
|
  cowboyro
join:2000-10-11 Shelton, CT | reply to Johnny Or straight into mysql: 'November 3rd, 2009 05:10:05PM' |
|
  Johnny Premium join:2001-06-27 Atlanta, GA | reply to cowboyro Looks good. I wonder why it says that it offsets it relative to the current time? |
|