I am trying to find a function to display to get a month's date from today. So if today is 7 may, the next month I want to get is 7 June.
I tried using this code
Carbon::now() + 30 but it doesn't help with what I want to achieve.
PS: new to Laravel and sorry about my bad English
use addMonth():
Carbon::now()->addMonth();
You can do it like this:
$dt = Carbon::now(); // This will assign current date into $dt
echo $dt->addMonth(); // this will print next month's date.
also if you want add how many months if you want.
$dt = Carbon::now();
$dt->addMonths(3); //After 3 months
$dt->subMonth(3); //Before 3 months
All tips are here carbon.nesbot.com/docs/
Related
I need to make a query in eloquent that brings me the records dated for the day after the current day. In other words, I need today, Friday 7/1, to load the records whose date entered in the variable $fechaip is equal to the following day. But, not taking into account Saturdays and Sundays. Is it possible to do something like this? To get the current date I suppose I can use Carbon... for example something like:
$today = Carbon\Carbon::now()
and then maybe use
$date = $today->addDay();
I think this would work, but how would I except Saturdays and Sundays?
// Starts on next day
$date= Carbon::tomorrow();
// Format N = 1 - for mondays, 7 - for sundays, here you can add conditions to ignore some days.
// If 6 or 7 (saturday or sunday), pass to the next day.
while (in_array($date->format('N'), [6, 7])) {
// Next day
$date->addDays(1);
}
$today = Carbon\CarbonImmutable::now(); // use immutable so $today is not modified
$date = $today->addWeekday();
I have a condition in laravel where I have to put a min date on html input type date but the condition is complex.
Condition :
1 : User should only be able to select date from past ten days.
2 : Only two days from previous month. Example lets today date is 4 april then the user should
only be able to select from 4 april to down 1 april and 2 days previous month.
so I can put the min="some date" in input of date
You didn't specify how you actually set the date, but here is a PHP version that should get you closer. All you need to do is extract the parts you need from $minDate.
$lastTwo = new DateTime(date('Y-m-d', strtotime(date('Y-m-1') . ' -2 days')));
$lastTen = new DateTime(date('Y-m-d', strtotime('-10 days')));
$minDate = max($lastTen, $lastTwo);
Use Carbon::now()->month; to get current month and make conditon according to your query.
I have a problem here I want to get the +4 week date from the current date using carbon, the +4 week plan will be dynamic depending on the input entered by the user, how do I make it, I've tried using this code but it's time to step back
$dt = Carbon::now();
dd($dt->week(4)->format('Y-m-d'));
Check Carbon docs, you may use addWeeks():
$dt = Carbon::now();
dd($dt->addWeeks(4)->format('Y-m-d'));
The week() method you've used, sets the week number using given first day of week and first day of year included in the first week.
I am not sure to understand your question, but I guess you just have to use :
$dt = Carbon::now();
$dt->addWeeks(4);
dd($dt->format('Y-m-d');
You don't need carbon for such simple tasks.
With DateTime
echo date_create('+4 weeks')->format("Y-m-d");
or with date and strtotime
echo date("Y-m-d",strtotime('+4 weeks'));
I have had a custom wordpress plugin that runs a report for the database entries from the previous few months, and it worked all year, but now that the new year has happened, it shows no results because it is counting back a few months but then keeping it for 2016.
For example, I load the page on Jan 1st 2016, and it shows the results for December 2016 instead of 2015 when I am using the following method for getting last month's data.
$today = date("Y-m-d H:i:s");
$month_current_start = date("Y-m-")."1 0:0:0";
$currentmonth = date("m");
$lastmonthnum = $currentmonth - 1;
$last_month_start = date("Y-").$lastmonthnum."-1 0:0:0";
$last_month_end = date("Y-").$lastmonthnum."-31 0:0:0";
so then I have an SQL that says something like
$var = $wpdb->get_var( "SELECT Count(*) FROM `table` WHERE table.timestamp BETWEEN '$last_month_start' AND '$last_month_end' AND table.amount = 1200" );
Any tips on how to fix this query so that it knows that last month is 2015?
I'm no professional so I'd love beginner-style help :-)
I can make it work manually but i'd like the code to work for the next few months automatically (since the actual plugin calculates the last four months).
Easiest way would be to convert last month to a time stamp, and use that for creating the format you have in your DB. An example based on what you have so far would be:
$today = date("Y-m-d H:i:s");
$month_current_start = date("Y-m-")."1 0:0:0";
$ltime = strtotime("-1 month");
$last_month_start = date("Y-m",$ltime)."-1 0:0:0";
$last_month_end = date("Y-m",$ltime)."-31 0:0:0";
What this does, is it makes a timestamp for 1 month ago from when it is called in the line $ltime = strtotime("-1 month"); From there, it makes the format for the first day and last day of the month (assuming there are always 31 days of course ^^) based on the timestamp, which you'll use to provide the year and the month.
PHP Sandbox with the code example and it's output if you'd want to play around with how strtotime works.
In mysql you have the option to use year_month, see this for reference.
Get the current year and month using your code
update your query accordingly
or you can have your sql server do all the work for you by using the date_add function
Update your question with the db server you are using, then I can write the new query for you.
I'm trying to get the number of the month of the year by the number of a week of the year and the year.
So for example week 1 is in january and returns 1, week 6 is in february so I want 2.
I tried to go with date_parse_from_format('W/Y') but had no success (it's giving me errors).
Is there any way to go with date_parse_from_format() or is there another way?
print date("m",strtotime("2011-W6-1"));
(noting that in 2011, January has six weeks so week 6 (by some definitions) is in month 1).
Just wanted to add a note for the first answer, the week number should be 01-09 for Weeks 1 through 9 (it will always give month 1 if you don't add the leading zero)
date("m",strtotime("2011-W06-1"));
Using PHP DateTime objects (which is the preferred way of dealing with dates see links below for more info) you can accomplish it this way:
$dateTime = new \DateTime();
$dateTime->setISODate($year,$week);
$month = $dateTime->format('n');
Note that the following will not work as week "W" is not a supported format:
$month = \DateTime::createFromFormat("W/Y ", "1/2015")->format('n');
The format used by this method is the same supported by the function you where trying to use date_parse_from_format, hence the errors.
Why PHP DateTime Rocks
DateTime class vs. native PHP date-functions
strtotime notes
PHP/Architect's Guide to Date and Time Programming (Chapter 2)
Something like this will do, this is also tested and works:
function getMonthByNumber($number,$year)
{
return date("F",strtotime('+ '.$number.' weeks', mktime(0,0,0,1,1,$year,-1)));
}
echo getMonthByNumber(27,2011);
Hope this helps