0

I have a very simple code for a cron job that makes a date entry into an SQL DB:

$qry_cron_test  = "INSERT INTO ".$tblprefix."cron_test SET

    create_datetime = '".date("Y-d-m H:i:s")."'";    

    $rs_cron_test   = $db -> Execute($qry_cron_test);

The problem is the following:

Between 1st and 12th of every month the date entry is like this - 2014-10-03 07:30:39, which is what i want.

However, when the current date is between 13th and the end of the month, the date entry looks like this - 0000-00-00 00:00:00. Then when 1st comes the entires are all ok again.

I tested this on couple of servers and also locally on Xampp always with the same result.

Any suggestions? What could be possibly wrong?

3 Answers 3

4

You have month and day the wrong way around.

$qry_cron_test  = "INSERT INTO ".$tblprefix."cron_test SET
    create_datetime = '".date("Y-m-d H:i:s")."'";    
    $rs_cron_test   = $db -> Execute($qry_cron_test);

date("Y-m-d H:i:s")

3

I recommend that, unless you need milisecond information, you always store date information in Unix Timestamp. It is lighter to store, since it is only a integer value, is faster to retrieve and is universal, since it is always based on UTC.

Specially in PHP, converting date information to (time() and strtotime) and from (date()) a unix timestamp is pretty easy. This way no matter where your user is, you can always show correct information in local time with almost no effort.

1

Wouldn't it be simpler to just do this:

insert into cron_test
create_datetime
values
(current_timestamp)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.