0

I am using this to store DATETIME in mysql

validation.php

//more code
$date = new DateTime("2012-01-01 23:59:59", new DateTimeZone('Europe/Paris'));
$dt = $date->format('Y-m-d H:i:s');

//and I am posting into my DATETIME field:

Insert...Value($dt)

1.. I get no errors but the date/time posted is 0000-00-00 00-00-00

What am I doing wrong?

2.. Is there a way to store date formatted as DD-MM-YYYY?

Pavlos1316
  • 474
  • 10
  • 25

3 Answers3

0

You can set timezone with PHP before store to database

$date = new DateTime("2012-07-05 16:43:21", new DateTimeZone('Europe/Paris')); 
Hiren Soni
  • 574
  • 3
  • 11
  • So if I use `$date = new DateTime("2012-07-05 16:43:21", new DateTimeZone('UTC+2'));` and then post $dt the time stored will be in UTC+2 format? This "2012-07-05 16:43:21" can be anything? – Pavlos1316 Jul 19 '12 at 06:40
  • yes, you are right. But you can not write "UTC+2" you have get your timezone name like "Europe/Paris" – Hiren Soni Jul 19 '12 at 06:48
  • I add the line you told me but now I get error **Object of class DateTime could not be converted to string** – Pavlos1316 Jul 19 '12 at 11:00
  • it works for me very simply $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru')); echo $date->format('Y-m-d H:i:sP') . "\n"; – Hiren Soni Jul 19 '12 at 11:11
0

You have to post

$var_date = $date->format('Y-m-d H:i:sP') 

not directly $date

Hiren Soni
  • 574
  • 3
  • 11
0

Did it... Correct syntax is:

$date = new DateTime(null, new DateTimeZone('Europe/Paris')); 
$dt = $date->format('Y-m-d H:i:sP');
Pavlos1316
  • 474
  • 10
  • 25