How to get Current Timestamp from Carbon in Laravel 5

✔ Recommended Answer

You can try this if you want date time string:

use Carbon\Carbon;$current_date_time = Carbon::now()->toDateTimeString(); // Produces something like "2019-03-11 12:25:00"

If you want timestamp, you can try:

use Carbon\Carbon;$current_timestamp = Carbon::now()->timestamp; // Produces something like 1552296328

See the official Carbon documentation here.

Method #2

To get the current timestamp using Carbon in Laravel 5, you can use the static now() method of the Carbon class.

Here's an example of how to get the current timestamp using Carbon:

php
use Carbon\Carbon; $currentTimestamp = Carbon::now();

In this example, we import the Carbon class using the use statement, and then call the now() method of the Carbon class to get the current timestamp. The now() method returns a new Carbon instance representing the current date and time.

You can also specify the timezone for the current timestamp by passing a timezone identifier as a parameter to the now() method. For example, to get the current timestamp in the America/New_York timezone:

php
$currentTimestamp = Carbon::now('America/New_York');

In this example, we pass the America/New_York timezone identifier as a parameter to the now() method to get the current timestamp in the Eastern Timezone. You can replace this with any valid timezone identifier that is supported by PHP.

Comments

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755