Search code examples
phplaravelphp-carbon

Carbon milliseconds timestamp using UNIX time


I'm working with an API that need the time with this format: "currentTime": 1534431933321 - unit is in MS.

How can I create that in Laravel using carbon?

And how to set my host time with getting that from endpoint?


Solution

  • What you are looking for is called "UNIX Time", can accomplish this with Carbon:

    //$nowInMs: returns 1534431933321
    $nowInMs = now()->getTimeStampMs();  
    
    //$letsParseValue: returns 1674799200000
    $letsParseValue = \Carbon\Carbon::parse('2023-01-27 10:00:00')->getTimeStampMs(); 
    

    For reference on other getters use: https://carbon.nesbot.com/docs/#api-getters

    To verify the times use this website: https://currentmillis.com/