0

I have clients from different many timezone, and I need analyze their system logs which the date is printed in their local machine timezone.

example: Mon Apr 03 00:50:15 CST 2017

actually I don't care about their timezone, what I need to print in my application only their local time, so the printout in my apps side must be: 2017-04-03 00:50:15.

how to convert the given date without concern about their timezone or my local server timezone or without have to set date_default_timezone_set in my apps? is there any php function to do that?

I want to avoid use string manipulation as much as possible.

thank you in advance.

BR//G

guete
  • 79
  • 2
  • 12

3 Answers3

1

Well, for all time-related operations, PHP expects you to set a default timezone (otherwise warnings are given). However, I think you're simply looking for a formatting operation.

<?php
date_default_timezone_set("UTC");
$source = "Mon Apr 03 00:50:15 CST 2017";
$dateOb = new DateTime($source);
print_r($dateOb);
echo $dateOb->format("Y-m-d H:i:s");

Output:

DateTime Object
(
    [date] => 2017-04-03 00:50:15.000000
    [timezone_type] => 2
    [timezone] => CST
)
2017-04-03 00:50:15
Ben
  • 611
  • 4
  • 10
0

you can use date_format if you only want to format your date.

$date=date_create("Mon Apr 03 00:50:15 CST 2017");
echo date_format($date,"Y-m-d H:i:s");
Chinito
  • 1,085
  • 1
  • 9
  • 11
  • it will give you warning : PHP Warning: date_create(): It is not safe to rely on the system's timezone settings. – guete May 05 '17 at 06:58
0

You need to set your desired timezone to be used by all date/time functions by setting your timezone identifier using date_default_timezone_set('parameter');. Find the list of supported timezones at http://php.net/manual/en/timezones.php. And then use date_default_timezone_get(); to get your given timezone

<?php
date_default_timezone_set('Asia/Manila'); 
date_default_timezone_get(); 
echo date('Y-m-d H:i:s') ;
?>
Carl Angelo Nievarez
  • 573
  • 1
  • 11
  • 33