PHP annoyance: MySQL now() vs PHP time()

January 23, 2008 § 3 Comments

When your DBA is thinking differently than you, you might get:

MySQL

field: created_on

type: DATE

while, on PHP side:

$created_on = time();

Below is simple script that get rid of such annoyance.

From PHP to MySQL:

$created_on = date(‘Y-m-d H:i:s’, time());

From MySQL to PHP:

$query = “SELECT created_on FROM some_table WHERE id=1”;

$row = mysql_fetch_array(mysql_query($query));

$created_on = $row[‘created_on’]; //created_on from MySQL in DATE format

list($date, $time) = explode(‘ ‘, $created_on);

list($y, $m, $d) = explode(‘-‘, $date);

list($h, $i, $s) = explode(‘-‘, $time);

$created_on = mktime($h, $i, $s, $m, $d, $y); //created_on timestamp

Advertisements

§ 3 Responses to PHP annoyance: MySQL now() vs PHP time()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading PHP annoyance: MySQL now() vs PHP time() at RAPD.

meta

%d bloggers like this: