Asked  7 Months ago    Answers:  5   Viewed   101 times

I have done a little experiment on php's DateTime class. In the documentation they have suggested the syntax for creating the object of DateTime class as given.

Object oriented style:

public DateTime::__construct() ( [ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

Procedural style:

DateTime date_create ( [ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

Returns new DateTime object.

Here the first argument they have specified as mandatory, as a date/time string as specified in Date and Time Formats section. Or we have to pass NULL here to obtain the current time when using the $timezone parameter.

But my problem is when I am giving following code:

$d_ob = new DateTime('x');
echo $d_ob->format('Y-m-d');

It's supposed to generate exception, but it's echoing the current date time like -


I am not getting the point, how it's working?



Apparently, this is not a bug, it is 'expected, but undocumented behaviour'See the comments to the bug report. All single letters (except for 'j') represent military timezones, see some code to demonstrate this.

There is more information here.

From RFC822

The military standard uses a single character for each zone. "Z" is Universal Time. "A" indicates one hour earlier, and "M" indicates 12 hours earlier; "N" is one hour later, and "Y" is 12 hours later. The letter "J" is not used.

So to answer your question

I am not getting the point, how it's working?

When DateTime::__construct() is passed a single value that is not a valid time string it assumes that the first parameter has been omitted and tries to parse the string as a time zone. As the RFC explains, 'x' is a valid timezone, so you will get a DateTime instance that is in timezone 'X'.

I should mention that although the constructor of DateTime recognises these single letter zones, the constructor of DateTimeZone does not!

I hope that helps.

Saturday, May 29, 2021
answered 7 Months ago

DateTime::createFromFormat and date_parse_from_format have been added in PHP 5.3 because there was a high demand for that feature, especially from developpers who code for users who don't use US date/time formats.

Before those, you had to develop a specific function to parse the format you were using ; with PHP < 5.3, what is generally done is :

  • Decide which format will be accepted by the application
  • Display some message saying something like "your input should be JJ/MM/AAAA" (French for DD/MM/YYYY)
  • Check that the input is OK, regarding to that format
  • And parse it to convert it to a date/time that PHP can understand.

Which means applications and developpers generally didn't allow for that many formats, as each format meant one different additionnal validation+parsing function.

If you really need that kind of function, that allows for any possible format, I'm afraid you'll kind of have to write it yourself :-(

Maybe taking a look at the sources of date_parse_from_format could help, if you understand C code ? It should be in something like ext/date/php_date.c -- but doesn't seem to be that simple : it's calling the timelib_parse_from_format function, which is defined in ext/data/lib/parse_date.c, and doesn't look that friendly ^^

Friday, May 28, 2021
answered 7 Months ago

You can use the datetime object or their function aliases for this:

Example (abridged from PHP Manual)


$datetime = new DateTime('2008-08-03 12:35:23');
echo $datetime->format('Y-m-d H:i:s') . "n";
$la_time = new DateTimeZone('America/Los_Angeles');
echo $datetime->format('Y-m-d H:i:s');

Edit regarding comments

but i cannt use this method because i need to show date in different time zones as the user login from different locations

That's not a problem. When a user logs in, you determine his timezone and set it to your DateTime object just like shown. I'm using a similar approach in one of my projects and it works like a charm.

in the database i need to get the dates in any single timezone, then only it can be processed properly

You store the time either as a timestamp or a datetime in one timezone. When you query a DateTime field, you either convert the time in a DateTime object to this timezone or - if your db supports it - query with the selected timezone.

Tuesday, June 1, 2021
answered 7 Months ago

I ran into the same issue and found the following solution in the documentation:

To run your functional tests, the WebTestCase class bootstraps the kernel of your application. In most cases, this happens automatically. However, if your kernel is in a non-standard directory, you'll need to modify your phpunit.xml.dist file to set the KERNEL_DIR environment variable to the directory of your kernel:

    <!-- ... -->
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    <!-- ... -->

So check your phpunit.xml.dist configuration file and try to add the absolute path to your app-directory.

Hope it helps.

Friday, July 30, 2021
Bálint Molnár
answered 5 Months ago

I got the solution, so i would like to share

Actually I have set default timezone in file as below


then I check the current time and timezone of MySQL server from phpmyadmin with below query

SELECT NOW(), SYSDATE(), @@global.time_zone , @@session.time_zone , 
      TIMEDIFF( NOW( ) , CONVERT_TZ( NOW( ) , @@session.time_zone ,  '+00:00' )) 

This return the OFFSET value +05:30

solution steps:

  • First I changed the timezone of mySQL Server to GMT/UTC +00:00 ( I have super privilage on mySQL server)

    SET GLOBAL time_zone = '+00:00';  
  • We save the date and time using start_date = NOW() ( column datatype: DATETIME )

Now there is 2 way to get date and time as per user's timezone (America/Denver)

first method ( using PHP DateTime)

   * first set timezone as GMT.
   * This is MUST because default timezone is differ from user timezone  
    $gmt = new DateTimeZone('GMT');
    $user_tz = 'America/Denver';
    $st = new DateTime($row[`start_date`], $gmt);
    // now set user timezone
    $st->setTimezone($user_tz );
    $stime = $qt->format('r');
    echo $stime;

second method (using MySQL UNIX_TIMESTAMP)

#$retrieve data from server in timestamp
$qry = "SELECT `start_date`,UNIX_TIMESTAMP(`start_date`) AS sTimestamp FROM..."  
$st = new DateTime('@'.$row['sTimestamp ']);                     
$stime = $st->format('r');
echo $stime;

Note : dont change start_date to timestamp with strtotime(). It will return different value from the UNIX_TIMESTAMP() i.e.

 strtotime($row['start_date']) !== $row['sTimestamp']
Saturday, August 7, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :