php - getting date time using DateTime return different value from date() function
Asked Answered
S

3

8

I am trying to get the current timestamp using Carbon or DateTime Class I get wrong date but when I use date() function it return the correct date I run the code on win server 2012 this is my code

dd([
    'Carbon::now()->format("Y-m-d H:i:s P")' => Illuminate\Support\Carbon::now()->format('Y-m-d H:i:s P'), 
    'DateTime()->format("Y-m-d H:i:s P")' => (new DateTime())->format('Y-m-d H:i:s P'), 
    'date("Y-m-d H:i:s P")' => date('Y-m-d H:i:s P'),
    'date("Y-m-d H:i:s P",microtime(true))' => date("Y-m-d H:i:s P", microtime(true)),
    'date("Y-m-d H:i:s P", time())' => date("Y-m-d H:i:s P", time()),
    'Carbon::now()->getTimestamp()' => Illuminate\Support\Carbon::now()->getTimestamp(),
    'microtime()' => microtime(true),
    'time()' => time(),
]);

this is the output that I got

[
  "Carbon::now()->format("Y-m-d H:i:s P")" => "2018-06-25 22:41:59 +03:00"
  "DateTime()->format("Y-m-d H:i:s P")" => "2018-06-25 22:41:59 +03:00"
  "date("Y-m-d H:i:s P")" => "2018-06-19 11:59:22 +03:00"
  "date("Y-m-d H:i:s P",microtime(true))" => "2018-06-25 22:41:59 +03:00"
  "date("Y-m-d H:i:s P", time())" => "2018-06-19 11:59:22 +03:00"
  "Carbon::now()->getTimestamp()" => 1529955719
  "microtime()" => 1529955719.4257
  "time()" => 1529398762
]

the server time is the same as the value of date function

Sebastian answered 19/6, 2018 at 9:24 Comment(10)
Hi, are you doing this on a local server, if so have you checked the server date time?Missi
its not on local serverSebastian
have you tried with: $dt = Carbon::now(); and then use Carbon::createFromFormat('Y-m-d H', $dt)->toDateTimeString();Rubicund
it cause Trailing data exceptionSebastian
probably a formatting issue; what if you run "Y-m-d H:i:s P" without the P, i.e. "Y-m-d H:i:s"Rubicund
P used to show +03:00Sebastian
date("Y-m-d H:i:s P", microtime(true))returns exact time DateTime() does. Is it wrong?Erethism
date("Y-m-d H:i:s P", microtime(true)) return wrong date as will as the DateTime classSebastian
Altogether it seems that the problem may be due to and old bug with monotonic value of world time (jamie comment at php.net/manual/ru/function.microtime.php). Can you check what he’s saying and if it applies to your server?Dorcia
Try to clear cache and then try again.Immodest
E
2

This is a configurations issue. You have 2 ways to get UNIX-timestamp at PHP: microtime(true) and time(). Carbon and DateTime are using microtime(true) internally.

Here are some details referring to their implementation: https://mcmap.net/q/1473552/-php-time-and-microtime-do-sometimes-not-concord

Einkorn answered 10/7, 2018 at 8:6 Comment(2)
The information in the linked answer is awesome but... Can this explain a 6-day difference? (This is an honest question!)Belshazzar
@ÁlvaroGonzález It's hard to debug without code and environment. I think it's a bug in PHP for Windows Server 2012. We can trace microtime() to function get_time_func: lxr.room11.org/xref/php-src%40master/win32/time.c#33 Here you can see special if statement for Windows Server 2012 and Windows 8, looks like it has a bug or something like this. If you can reproduce it would be helpful to report it at bugs.php.netEinkorn
H
0

Have you tried setting the timezone at the top of the script? You might be running into a session vs server timezone issue:

date_default_timezone_set ('whatevertimezone');
Historiographer answered 12/7, 2018 at 22:31 Comment(1)
I do not think the timezone is the problem because timezone will change the time not the date 2018-06-19 - 2018-06-25 as shown in the question despite that I tried to change the timezone the problem still appearSebastian
U
-1

can you try using

Carbon::today() 

instead of using Carbon::now. It works for me .

Uganda answered 12/7, 2018 at 22:25 Comment(1)
if I want another way to get the correct date I can use date function but I have to fix the problem because each package uses DateTime or Carbon Class will get wrong dateSebastian

© 2022 - 2024 — McMap. All rights reserved.