I couldn't find anywhere in documentation how to show current year or month with Carbon?
when i write this:
Carbon\Carbon::now('m');
it gives me the whole time stamp, but I just need the month
like
date('m');
but it must be Carbon!
How can I achieve this?
$now = Carbon::now();
echo $now->year;
echo $now->month;
echo $now->weekOfYear;
Update:
even this works since Laravel 5.5^
echo now()->month
I think you've already worked this out in a comment, but just for clarity: Carbon extends PHP's native DateTime class, so you can use any of the methods available on that, like format:
Carbon::now()->format('M');
(where M is the modifier for A short textual representation of a month, three letters)
You can use these both ways to get the current month
Carbon::now()->month;
or
Carbon::now()->format('m');
Just use this in your any blade file for print year:
{{ \Carbon\Carbon::now()->year }}
I wanted to get the current month and got to this question, to get the current month:
$now = Carbon::now();
$monthStart = $now->startOfMonth();
w/ Carbon + Laravel:
now()->format('M')
use Carbon\Carbon;
$today= Carbon::now();
echo $today->year;
echo $today->month;
echo $today->day;
echo $today->hour;
echo $today->minute;
echo $today->second;
Laravel 8
return date('m');
or
return now()->format('m');
You cant call it statically
use
$now = Carbon::now();
echo $now->month
Related
I'm moving data from the old project to my laravel project the start date format was in this format 1496242424 ,1496269010 How I convert it to time
To a Carbon object which can be saved on your models, if you add the field to the $dates property.
Carbon\Carbon::createFromTimestamp(1496242424);
$fdate=$request->Fdate;
$tdate=$request->Tdate;
$start = Carbon::parse($fdate)->format('Y/m/d');
$end = Carbon::parse($tdate)->format('Y/m/d');
$days = $end->diffInDays($start);
echo $days;
exit;
you can convert like this
<?php
echo $currentDate = date('Y-m-d H:i:s', 1496242424);
?>
output :
2017-05-31 16:53:44
It is very simple use date function in php To convert this to time use below code
echo date('Y-m-d',1496242424);
output of above code will be
2017-05-31
and to convert this to datetime use below code:
echo date('Y-m-d h:i:s',1496242424);
output of above code will be
2017-05-31 02:53:44
Thanks
why convert it to dateTime. keep it as unix timestamp. Just add a method in your model to tell it to use unix timestamp
class User extends Model
{
protected $dateFormat = 'U';
}
Use this code in your datepicker and the format is changed as per your own choice
$(function() {
$(".datepicker").datepicker({
dateFormat: "dd/mm/yy",
Set this line in your controller
$post->start_date =Carbon::parse(strtotime($request->start_date))->format('Y-m-d');
You should use carbon library use Carbon/Carbon;
I have this php code that I want to output the current year, month and day.
date_default_timezone_set('Sweden/Stockholm');
$time_info = getdate();
$day = $time_info['mday'];
$month = $time_info["mon"];
$year = $time_info['year'];
$date = "$year, $month, $day";
And the output:
2014 10 29
But I want it to output
year-month-day
But when I change the $date to $date = $year."-".$month."-".$day."-"; the output is: 1975.
Obviously, this is wrong, so how can I fix it. And a explanation why this occurs would also be great.
EDIT:
Ok, so according to #Marc B and #Barry , it did at some point math, and they were right. I don't know why this occurs, but I got it sorted out. Thanks!
Why don't you use the php date function?
echo date("Y-m-d");
You can get that if you run this.
$time_info = getdate();
echo $time_info->format('Y-m-d');
Using PHP format() function resolves your problem.
Documentation is here.
use php date function Use Like this
date_default_timezone_set('Sweden/Stockholm');
echo date("Y-m-d");
I want to use this Carbon function:
Carbon::now()->subDays(5)->diffForHumans()
And I need to create the correct integer.
I am loading a string with a Datetime, which I want to subtract in Laravel like this:
$datetime = $score->created_at;
Then I save the current Time into a variable
$now = Carbon::now()->toDateTimeString();
This is what I get:
echo $now . '<br>'; // 2014-07-13 22:53:03
echo $datetime; // 2014-07-12 14:32:17
But when I want to subtract one from another I get the following error:
echo $now - $datetime;
Object of class Carbon\Carbon could not be converted to int
Any help here would be greatly apreciated.
I know it's a bit late, but this works:
$score->created_at->diffForHumans(\Carbon\Carbon::now())
If you want to change the date format just use the format function
$now = Carbon::now();
$score->created_at->diffForHumans($now)->format('Y-m-d');
I have been given year day (1-366) and I need to figure out which month it is in, how can I do this?
Well, I actually have a date string like : year, day or year, minute of day, second and I ultimately want to create a POSIX timestamp from it, how can I do this?
Thank you!
If you have PHP >= 5.3, then you can use DateTime::createFromFormat.
$day = 176;
$date = DateTime::createFromFormat('z', $day);
echo $date->getTimestamp(); // 1372275280
<?php
$year=2013;
$d=360;
echo date("m",strtotime("1/1/$year + $d days"))
?>
Use the date function to get a posix time stamp.
To get the month of a certain date, use intval(date('m'), mktime($h,$m,$s,$month,$day,$year))
I want to get today's date + one year. How do I achieve this with PHP's date functions?
echo date('Y', strtotime('+1 year'));
You can use strtotime and date
$date = '2010-09-16';
echo date('Y-m-d', strtotime("+12 months $date"));
// 2011-09-16
On a sidenote: DateTime questions like this have been answered over and over again, so you could have found how to add to a date easily by using the search function.
From PHP's documentation:
<?php
$date = new DateTime($your_supposed_date);
$date->add(new DateInterval('P1Y'));
echo $date->format('Y-m-d') . "\n";
?>
Gordon's much cleaner version (Thank you!):
<?php
$date = new DateTime("+12 months $theDate");
echo $date->format('Y-m-d') . "\n";
?>
$Ad_year = 2015-10-20
<?php echo $Ad_year + 1?>
Result 2016
The shortest version:
echo (int)date('Y') + 1;
You could use the new Datetime and Datetime_Intervall-classes introduced in the later PHP 5-versions.
I once posted an answer in this question. Maybe it helps you :)
The advantage is, that this classes also checks for leap-seconds and leap-years, timezones, etc.
If you're working with timestamps
echo time()+60*60*24*365
Best and easy solution...
You can change month or year or day.
date('Y-m-d',strtotime("+1 day +2months +1 year"));
Below code also return next year from current date:
<?php echo date('Y', strtotime('+12 month'));>