February 26, 2012

PHP & MySQL date

Question by Ayman Jitan

I am trying to insert in a field of type “timestamp” in MySQL database “CURRENT_TIMESTAMP” and its 2012-2-26 10:04:34 AM in my local are and its the time on my pc
but the thing is that I find it 2012-02-26 02:04:34 in my database
everytime I insert I find different values from my local time
and aslo if I do

echo strtotime('now');

it prints the same value “2012-02-26 02:04:34”

how can I fix , knowing that I am working on a schedule needs to perform operations on a certain time

Answer by Starx

This looks like problem with time zones. Check them in php.ini. Or as suggested my Cheery on the comment, use [docs here]

<?php
date_default_timezone_set('America/Los_Angeles');
?>

However, You should make sure you are using correct methods to input the date and time in the database. There are two ways to do so.

  1. MYSQL: Use NOW() Function

    INSERT INTO table VALUES( data = '$data', date = now());
    
  2. PHP: Use time() Function

    $query = "INSERT INTO table VALUES( data = '$data', date = '".time()."');"
    

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!