[php] Getting Current date, time , day in laravel

I need to get the current date, time, day using laravel

I tried to echo $ldate = new DateTime('today'); and $ldate = new DateTime('now');

But it is returning 1 always.

How can i get the current date, time , day in larvel

This question is related to php date laravel

The answer is


use DateTime;

$now = new DateTime();

You can try this.

use Carbon\Carbon;

$date = Carbon::now()->toDateTimeString();

If you want to use datetime class

$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s');

It's very simple:

Carbon::now()->toDateString()

This will give you a perfectly formatted date string such as 2020-10-29.

In Laravel 5.5 and above you can use now() as a global helper instead of Carbon::now(), like this:

now()->toDateString()

$dayOfYear = today()->dayOfYear; $dayOfWeek = today('Europe/London')->dayOfWeek;


//vanilla php
Class Date {
    public static function date_added($time){
         date_default_timezone_set('Africa/Lagos');//or choose your location
        return date('l F Y g:i:s ',$time);

    }


}

FOR LARAVEL 5.x

I think you were looking for this

$errorLog->timestamps = false;
$errorLog->created_at = date("Y-m-d H:i:s");

How about

    $date = Carbon::now();
    return $date->toArray();

will give you

{
  "year": 2019,
  "month": 1,
  "day": 27,
  "dayOfWeek": 0,
  "dayOfYear": 26,
  "hour": 10,
  "minute": 28,
  "second": 55,
  "englishDayOfWeek": "Sunday",
  "micro": 967721,
  "timestamp": 1548570535,
  "formatted": "2019-01-27 10:28:55",
  "timezone": {
    "timezone_type": 3,
    "timezone": "Asia/Dubai"
  }
}

The same props are accessible through


    return [
             'date' => $date->format('Y-m-d'),
              'year' => $date->year,
              'month' => $date->month,
              'day' => $date->day,
              'hour' => $date->hour,
              'isSaturday' => $date->isSaturday(),
          ];


If you want date of today

use namespace

use Carbon\Carbon as time;

code ,

 $mytime=time::now();
 $date=$mytime->toRfc850String();
 $today= substr($date, 0, strrpos($date, ","));
 dd($today)

output , "Sunday"


I used a function to get current datetime

 protected function getCurrentDate()
    {
        $now = Carbon::now();
        return $now->toDateTimeString('');
    }

However I get an error.

Carbon\Exceptions\UnitException
Precision unit expected among: minute, second, millisecond and microsecond.

Any ideas how to get the time in a format like day.month.Year Hour:minute


Try this,

$ldate = date('Y-m-d H:i:s');

Here is another way to do this

Use \Carbon\Carbon;

$date = Carbon::now();

echo $date->toRfc850String();

Output will be like this

Saturday, 11-May-19 06:28:04 UTC

You have a couple of helpers.

The helper now() https://laravel.com/docs/7.x/helpers#method-now

The helper now() has an optional argument, the timezone. So you can use now:

now();

or

now("Europe/Rome");

In the same way you could use the helper today() https://laravel.com/docs/7.x/helpers#method-today. This is the "same thing" of now() but with no hours, minutes, seconds.

At the end, under the hood they use Carbon as well.


Php has a date function which works very well. With laravel and blade you can use this without ugly <?php echo tags. For example, I use the following in a .blade.php file...

Copyright © {{ date('Y') }}

... and Laravel/blade translates that to the current year. If you want date time and day, you'll use something like this:

{{ date('Y-m-d H:i:s') }}

You can set the timezone on you AppServicesProvider in Provider Folder

public function boot()
{
    Schema::defaultStringLength(191);
    date_default_timezone_set('Africa/Lagos');
}

and then use Import Carbon\Carbon and simply use Carbon::now() //To get the current time, if you need to format it check out their documentation for more options based on your preferences enter link description here


After Laravel 5.5 you can use now() function to get the current date and time.

In blade file, you can write like this to print date.

{{  now()->toDateTimeString('Y-m-d') }}

enter image description here


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to date

How do I format {{$timestamp}} as MM/DD/YYYY in Postman? iOS Swift - Get the Current Local Time and Date Timestamp Typescript Date Type? how to convert current date to YYYY-MM-DD format with angular 2 SQL Server date format yyyymmdd Date to milliseconds and back to date in Swift Check if date is a valid one change the date format in laravel view page Moment js get first and last day of current month How can I convert a date into an integer?

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory