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.
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 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/
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.
Hello i am writing a module in PHP. Here I have to check for the condition like if user select demo for application with basic features he should get demo for next day till 12 PM. I am using code
date_default_timezone_set("Asia/Kolkata");
echo date("Y-m-d 12:00:00",strtotime('tomorrow'))
it is working fine. But when user try for demo account in mid night that is same date as next day. it is giving me the day after tomorrow date. For example today is 22/01/2014 before 22/01/2014 11:59PM it is working fine. but 23/01/2014 00:01 AM it is giving date 24/01/2014 12:01PM but i want it to be 23/01/2014 12:01PM. Any suggestion.
date_default_timezone_set("Asia/Kolkata");
$day = date('H') < 12 ? 'today' : 'tomorrow';
echo date("Y-m-d 12:00:00",strtotime($day));
The key is to work out which day the demo should end, this can be done by checking the current hour. If the current hour is less than 12, then it should be today's date, otherwise tomorrow's.
I ask user to enter start and end date in a certain application. Then I calculate the no. of weeks in between these two dates.
I want to fetch documents created per week in the given period of time.
I am unable to implement a logic which will help me to fetch first week, second week, third week, and so on.
I will use this as input to database and then create graph. I am using php as the server side language. Anyone could suggest me the Algorithm.
ex:
Start date: 20/07/2012
End Date: 19/02/2013.
Total no. of days: 214.
Total no. of weeks: 30 (30 weeks and 4 days)
I want to fetch period of 20/07/2012 to 27/07/2012 and no. of Docs during this period. Till 15/02/2013.
Without seeing your database structure here is a sample of what you require
SELECT YEAR(doc_created) AS report_year, WEEKOFYEAR(doc_created) AS report_week, COUNT(*) AS doc_count
FROM documents
WHERE doc_created BETWEEN '2012-07-20' AND '2013-07-19'
GROUP BY YEAR(doc_created), WEEKOFYEAR(doc_created)
grouping by weekofyear is not enough, as period may overlap the next year, so we should also include the year.
possible results:
year week docs
2012 1 4
2012 3 1
2013 1 65
2013 2 4
as you can see 2012 week 1 would overlap 2013 week 1, if we just used week number.
for your grid/graph use the reference (year wk) and amount (docs)
e.g. 2012wk1 is 4, 2012wk3 is 1
Try this code:
$start_date = "20-07-2012";
$end_date = "20-08-2012 ";
while($start_date){
if(strtotime($start_date) > strtotime($end_date)){
exit;
}
$start_date = strtotime("+7 day", strtotime($start_date));
$start_date = date("d-m-Y", $start_date);
echo $start_date;
echo "<br/>";
}