You are on page 1of 3

Getting Current date, time , day in laravel

Ask Question
Asked 5 years, 2 months ago
Active 11 days ago
Viewed 337k times
103

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


php date laravel
shareimprove this question
asked Jan 23 '15 at 11:44
AngularAngularAngular
2,29955 gold badges1616 silver badges3333 bronze badges

4
Use date(). No need to ovecomplicate easy things, add unnecessary overhead. –
Mārtiņš Briedis Jan 23 '15 at 12:20
1
I think the main problem is that you're doing echo $now = new DateTime();
whereas instead you should just set the variable without the echo (i.e. do $now =
new DateTime();) and then when you want to echo it you need to use the format()
method (docs): echo $now->format('Y-m-d'); – alexrussell Jan 23 '15 at 12:21

add a comment
13 Answers
Active
Oldest
Votes
197

Laravel has the Carbon dependency attached to it.

Carbon::now(), include the Carbon\Carbon namespace if necessary.

Edit (usage and docs)

Say I want to retrieve the date and time and output it as a string.

$mytime = Carbon\Carbon::now();
echo $mytime->toDateTimeString();

This will output in the usual format of Y-m-d H:i:s, there are many pre-created
formats and you will unlikely need to mess with PHP date time strings again with
Carbon.

Documentation: https://github.com/briannesbitt/Carbon

String formats for Carbon: http://carbon.nesbot.com/docs/#api-formatting


shareimprove this answer
edited Dec 9 '16 at 2:27
chepe263
2,4181717 silver badges3535 bronze badges
answered Jan 23 '15 at 11:57
Everon
2,54111 gold badge88 silver badges1212 bronze badges

3
use Carbon\Carbon; is the namespace declaration – Kiren Siva May 4 '15 at 7:07

how to extract dayname from 2016/2/20 in laravel 5?? please reply – sabin
maharjan Mar 20 '16 at 5:26
6
@AngularAngularAngular Use Carbon::now()->format('d-m-Y') – EThaizone Jo Sep 21
'16 at 4:52
1
catch up on your Carbon documentation. No one will write code for you. The
answer provides what was asked by the OP. – kratos Jan 31 '17 at 16:00

add a comment
56

Try this,

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

shareimprove this answer


answered Jan 23 '15 at 11:46
Vinod VT
5,72099 gold badges4444 silver badges6868 bronze badges

13
@Chennai Why do you need it to do the "laravel" way? Laravel is the "php
way" :) – Mārtiņš Briedis Jan 23 '15 at 12:14
2
When you have access to classes like Carbon that are fully tested it's always
useful to utilise them. Why reinvent the wheel when there's already a robust
implementation that does what you need and more? – Everon Jan 23 '15 at 12:15
3
@Everon Using built in core functions is not reinventing the wheel. People
always want to overcomplicate things :S – Mārtiņš Briedis Jan 23 '15 at 12:18
3
You just set the right timezone in global config and that's it, the same way as
carbon works. It's just silly to do things "laravel way", because it looks more
fancy? How is this \Carbon\Carbon::now()->format('d.m.Y'); better than
date('d.m.Y') ? – Mārtiņš Briedis Jan 23 '15 at 12:25

2
Thanks So, the date('Y-m-d H:i:s') won't hurt by anyway right ? –
AngularAngularAngular Jan 23 '15 at 12:44

show 3 more comments


46

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') }}

shareimprove this answer


answered Feb 19 '17 at 7:28
Menasheh
2,52311 gold badge2525 silver badges3636 bronze badges
add a comment
15

If you want to use datetime class

$dt = new DateTime();


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

You might also like