Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting DATE TIMESTAMP Value to MySQL Using PHP

Using php I am inserting or updating the mysql database with create date or modified date using the variables

$datestring = "%Y:%m:%d %h:%i:%s";
$time = time();
$createdate= mdate($datestring, $time);

In this $createdate will be the variable I use to insert or update the table. But it's updating the wrong value. It's not the server time or localtime. Mostly it's 30 mins delay with the server's time.

like image 259
ASD Avatar asked Apr 07 '10 04:04

ASD


People also ask

How can I insert current date and time in MySQL using PHP?

To insert only date value, use curdate() in MySQL. With that, if you want to get the entire datetime, then you can use now() method. Insert both date and time with the help of now().

How do I insert date in YYYY-MM-DD format in MySQL?

You can use str_to_date to convert a date string to MySQL's internal date format for inserting.


2 Answers

Use date() function of PHP

$createdate= date('Y-m-d H:i:s');
like image 102
Veer Avatar answered Nov 01 '22 18:11

Veer


Edit: after some googling it looks like you're using CodeIgniter. You should have mentioned that in your question.

The format string you're using doesn't match MySQL's date format. You want to use:

$datestring = '%Y-%m-%d %H:%i:%s';
like image 44
Chad Birch Avatar answered Nov 01 '22 17:11

Chad Birch