Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats wrong with DateTime object

Tags:

date

php

datetime

Can anyone tell what is wrong with the code.

$timezone = "Asia/Karachi"; 
$date = new DateTime($when_to_send, new DateTimeZone($timezone));
$date = $date->setTimezone(new DateTimeZone('GMT')); 
$when_to_send = $date->format('Y-m-d H:i:s');

error is: Call to a member function format() on a non-object

like image 437
Ayaz Alavi Avatar asked May 31 '10 12:05

Ayaz Alavi


2 Answers

$date = $date->setTimezone(new DateTimeZone('GMT'));

Makes the $date variable null, you should just call it:

$date->setTimezone(new DateTimeZone('GMT'));

like image 63
cpf Avatar answered Oct 02 '22 17:10

cpf


If you're not running at least PHP 5.3.0 (as written in the manual, which you surely read before asking, right?), setTimezone will return NULL instead of the modified DateTime. Are you running at least PHP 5.3.0?

like image 21
Matti Virkkunen Avatar answered Oct 02 '22 18:10

Matti Virkkunen