date into unix timestamp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

date into unix timestamp

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: date into unix timestamp

Post by AbraCadaver »

Depending on timezones and how you set them:

PHP

Code: Select all

strtotime($date)
MYSQL
[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.
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: date into unix timestamp

Post by blade_922 »

AbraCadaver wrote:Depending on timezones and how you set them:

PHP

Code: Select all

strtotime($date)
MYSQL
[text]UNIX_TIMESTAMP($date)[/text]
Hey

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)
go?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: date into unix timestamp

Post by AbraCadaver »

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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: date into unix timestamp

Post 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().
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: date into unix timestamp

Post 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.
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.
Post Reply