Page 1 of 1
date into unix timestamp
Posted: Mon Jan 17, 2011 1:16 pm
by blade_922
Hey,
okay so i have a variable $date that displays the date and time and that gets inserted into my mysql db. Is there a way to generate the unix timestamp for whatever the date is, so i can insert that into a seperate field in the mysql db at the same time as i insert the date.
The dates go all they way back to 2005
Kind Regards
Omar
Re: date into unix timestamp
Posted: Mon Jan 17, 2011 1:39 pm
by AbraCadaver
Depending on timezones and how you set them:
PHP
MYSQL
[text]UNIX_TIMESTAMP($date)[/text]
Re: date into unix timestamp
Posted: Mon Jan 17, 2011 2:11 pm
by blade_922
AbraCadaver wrote:Depending on timezones and how you set them:
PHP
MYSQL
[text]UNIX_TIMESTAMP($date)[/text]
Hey
Thanks, So let me get this right, in my php file i would put
The above converts it into the unix timestamp?
Where would
go?
Re: date into unix timestamp
Posted: Mon Jan 17, 2011 2:16 pm
by AbraCadaver
Code: Select all
mysql_query("INSERT INTO `table` (`date`, `unixtime`) VALUES ($date, UNIX_TIMESTAMP($date)");
Re: date into unix timestamp
Posted: Mon Jan 17, 2011 2:18 pm
by pickle
The MySQL function UNIX_TIMESTAMP() converts a MySQL date string (YYYY-MM-DD HH:MM:SS) into the equivalent UNIX timestamp. You don't need to use both.
The first option will convert the date to a unix timestamp & you can then put that timestamp into your query. The in the second option, you put the date into your query, and let MySQL convert it with the UNIX_TIMESTAMP function. PHP's strtotime() is much more flexible than MySQL's UNIX_TIMESTAMP() though, so unless your date string is in the MySQL format listed above, you'll need to use strtotime().
Re: date into unix timestamp
Posted: Mon Jan 17, 2011 2:20 pm
by AbraCadaver
pickle wrote:The MySQL function UNIX_TIMESTAMP() converts a MySQL date string (YYYY-MM-DD HH:MM:SS) into the equivalent UNIX timestamp. You don't need to use both.
The first option will convert the date to a unix timestamp & you can then put that timestamp into your query. The in the second option, you put the date into your query, and let MySQL convert it with the UNIX_TIMESTAMP function. PHP's strtotime() is much more flexible than MySQL's UNIX_TIMESTAMP() though, so unless your date string is in the MySQL format listed above, you'll need to use strtotime().
Yes, sorry, I forgot to put an OR in between the two examples.