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
date into unix timestamp
Moderator: General Moderators
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: date into unix timestamp
Depending on timezones and how you set them:
PHP
MYSQL
[text]UNIX_TIMESTAMP($date)[/text]
PHP
Code: Select all
strtotime($date)[text]UNIX_TIMESTAMP($date)[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: date into unix timestamp
HeyAbraCadaver wrote:Depending on timezones and how you set them:
PHPMYSQLCode: Select all
strtotime($date)
[text]UNIX_TIMESTAMP($date)[/text]
Thanks, So let me get this right, in my php file i would put
Code: Select all
$unixtimestamp = strtotime($date)The above converts it into the unix timestamp?
Where would
Code: Select all
UNIX_TIMESTAMP($date)- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: date into unix timestamp
Code: Select all
mysql_query("INSERT INTO `table` (`date`, `unixtime`) VALUES ($date, UNIX_TIMESTAMP($date)");mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: date into unix timestamp
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().
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().
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: date into unix timestamp
Yes, sorry, I forgot to put an OR in between the two examples.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().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.