Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in Laravel

How to convert Datetime to Unix timestamp in Laravel

🎈 🎈 🎈
+1

    use Carbon\Carbon;

    $dateString = '2008-06-14 23:19:12'; // Example date string
    $timestamp = Carbon::parse($dateString)->timestamp;
    
    echo $timestamp;                     
        

Output:
1213485552
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this code snippet, we are using Laravel's Carbon library to convert a human-readable date string into a Unix timestamp. The date string '2008-06-14 23:19:12' is passed to the Carbon::parse() method, which parses the string into a Carbon date object. From this object, we access the timestamp property, which converts the date into a Unix timestamp, representing the number of seconds since January 1, 1970 (the Unix epoch). In this case, it outputs the Unix timestamp '1213485552', which corresponds to the provided date and time.


Other useful Laravel date functions

Function Description Example
now() Returns the current date and time as a Carbon instance. now() returns 2024-09-15 10:00:00
today() Returns the current date with time set to midnight as a Carbon instance. today() returns 2024-09-15 00:00:00
yesterday() Returns the date for yesterday with time set to midnight as a Carbon instance. yesterday() returns 2024-09-14 00:00:00
Carbon::parse() Parses a date string and returns a Carbon instance. Carbon::parse('2024-09-15') returns 2024-09-15 00:00:00
Carbon::createFromTimestamp() Creates a Carbon instance from a Unix timestamp. Carbon::createFromTimestamp(1633024800) returns 2021-10-01 00:00:00
addDays() Adds a specified number of days to the Carbon instance. now()->addDays(5) returns 2024-09-20 10:00:00
subMonths() Subtracts a specified number of months from the Carbon instance. now()->subMonths(2) returns 2024-07-15 10:00:00
diffForHumans() Returns a human-readable difference between the current time and another date. now()->subDays(3)->diffForHumans() returns 3 days ago
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder