Professional Documents
Culture Documents
Ask Question
Asked 5 years, 2 months ago
Active 11 days ago
Viewed 337k times
103
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
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
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,
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
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') }}