Monday, February 13, 2012

0

PHP Get Current Datetime

I used this code when I want to save a date/time value for my "created" table field.

//you have to set it where it is appropriate for you
date_default_timezone_set('Asia/Manila'); 

$created = date('Y-m-d h:i:s');

echo $created;

Output:
2012-02-14 10:08:31


Sample Use:
$sql = "insert into 
your_table
( 
    created,
    name
)
values 
( 
    '" . $created . "',
    '" . escapeStr( $name ) . "'
)";

By the way, you can also use NOW()
VALUES
(
    NOW(),
    '" . escapeStr( $name ) . "'
)";

But it seemed like it doesn't work properly for me.

Value stored using $created:
2012-02-14 10:08:31

Value stored using NOW():
2012-02-14 02:08:53

In my case, using the $created value is just fine. I haven't tried setting proper timezone for using NOW(), anyone? :)
More
Powered by Blogger.