I'm suck on the logic for this, my goal is to execute if an item is with in a given date range.
I get yesterday's date and the date from 8 days ago with Carbon like so:
$dt = \Carbon\Carbon::yesterday();
$dtB = \Carbon\Carbon::yesterday()->subDays(8);
$today = $dt->toDateString();
$todayBack = $dtB->toDateString();
I then need to execute this if statement to find if the item in the database fits with in these time frames.
if($orderSet->item_clicked == 'printing' && $orderSet->completed_date == $today) {
// run some stuff here
}
Currently I can execute if it's today but I would like do in between these two days. In example. 09-20-19 - 10-09-19 in between these two dates. Just as an example.
Carbon has a between() method. Use the original carbonized dates instead of the date strings.
$dtCompleted = \Carbon\Carbon::parse($orderSet->completed_date);
if (if($orderSet->item_clicked == 'printing' && $dtCompleted->between($dtB, $dt)) {
// run some stuff
}
See the documentation of Comparison
Related
I am trying to compare to date to figure out how much time is between them, which I know how to do, date_diff(), but I want to then compare the time between the dates and if it is greater than 7 days do something and if not do something else. I think it sounds easy and I know there are probably fairly simple solutions to do so but I am just not a fan of dates and comparisons. Here is a snippet of what I got so far as it is just one case of a switch statement so the rest are basically identical.
$array = array();
$today = date("Y-m-d"); // get today's date
foreach($arrayOfObjs as $obj){
if ($obj->get("renewalDate") >= $today){
array_push($array, $obj->get("renewalDate"));
}else{
switch($obj->get("recurrencePeriod")){
case 1:
/*
* All cases follow same structure
* Build the date in format Y-m-d from renewalDate out of the obj.
* Loop through the date while it's less than today.
* After date is greater than today return date add to array
*/
$date = DateTime::createFromFormat("Y-m-d", $obj->get('renewalDate'));
while($date <= $today){
$date->add(new DateInterval('P7D'));
}
$diff = date_diff($today, $date);
if($diff->format('%a') <= 7){
$obj->renewalDate($date);
array_push($array, $obj);
}
break;
Basically, my database stores dates and those dates could be passed but it could be a reoccurring event. To calculate the next time that event would happen I check if the data in the database is before today's date and if it is then I continue to add the incremental amount (in this case 7 for a weekly reoccurring event) and compare the dates again. After the date that is incremented passes today's date I want to find out if it is within 7 days and if so add it to an array to get returned. I know... since I'm adding 7and it's within 7 days the first reoccurring event will always be within 7 days but that is not the case for monthly events or anything greater.
All cases are broken so I only included this one for simplicity. I can get date_Diff to return something like 7 or 12 or whatever the number may be but how can I check if that number is within the 7 days I want?
Thanks, I will include more information if needed to clarify any misunderstandings.
I'm not entirely sure what you are trying to do, but how about the following if you are just projecting dates forward and backwards and want to know if they are 7 days or more either way:
$today = date("Y-m-d");
$todaytime = strtotime($today);
$testdate = "2017-06-31";
$testtime = strtotime($testdate);
$back7days = strtotime("-7 days",$todaytime);
if($testtime < $back7days)
echo "X"; // do somthing if testdate was more than 7 days ago
$fwd7days = strtotime("+7 days", $todaytime);
if($testtime > $fwd7days)
echo "Y"; // do somthing if testdate is more than 7 days in future
Just make sure that you use less-than or less-than-and-equals comparators etc to handle the boundary conditions you need.
I'm using Carbon PHP library.The answer in the duplicate question uses PHP's built in function.
count how many days within a date range are within another date range
Below is the code that I use to find if a date range($userDateStart and $userDateEnd) is with in another date range ($couponStart and$couponEnd`) and it works fine without any error but I don't know how to find the days that overlap/exists that is in this date range?
The library I'm using is http://carbon.nesbot.com/docs/
The expected result should be 4 in this case..Hope you will help me.
$userDateStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$userDateEnd = Carbon::createFromFormat('Y-m-d','2015-06-29');
$couponStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$couponEnd = Carbon::createFromFormat('Y-m-d','2015-10-31');
if(($userDateStart >= $couponStart && $userDateEnd <= $couponEnd) ||
($couponStart >= $userDateStart && $couponEnd <= $userDateEnd)){
die("Yes,The date is within this date range");
}
die("No,It is not within this date range");
According to the Documentation that was provided, You need to use this:
$dt = Carbon::create(2012, 4, 30, 0);
echo $dt->diffInDays($dt->copy()->addMonth()); // 30
echo $dt->diffInDays($dt->copy()->addWeek()); // 7
So to work with your program I'm thinking you'll need to do this:
$userDateStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$userDateEnd = Carbon::createFromFormat('Y-m-d','2015-06-29');
$couponStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$couponEnd = Carbon::createFromFormat('Y-m-d','2015-10-31');
//Determin the highest date from the starts and the minimum dates from the ends
$startBetweenDate = $userDateStart->max($couponStart);
$endBetweenDate = $userDateEnd->min($couponEnd);
//Now find how many days are between
echo $startBetweenDate->diffInDays($endBetweenDate); //Should be 4
Please note: This was not tested as I do not have Carbon's library installed.
I am getting a date back from a mysql query in the format YYYY-MM-DD.
I need to determine if that is more than three months in the past from the current month.
I currently have this code:
$passwordResetDate = $row['passwordReset'];
$today = date('Y-m-d');
$splitCurrentDate = explode('-',$today);
$currentMonth = $splitCurrentDate[1];
$splitResetDate = explode('-', $passwordResetDate);
$resetMonth = $splitResetDate[1];
$diferenceInMonths = $splitCurrentDate[1] - $splitResetDate[1];
if ($diferenceInMonths > 3) {
$log->lwrite('Need to reset password');
}
The problem with this is that, if the current month is in January, for instance, giving a month value of 01, and $resetMonth is November, giving a month value of 11, then $differenceInMonths will be -10, which won't pass the if() statement.
How do I fix this to allow for months in the previous year(s)?
Or is there a better way to do this entire routine?
Use strtotime(), like so:
$today = time(); //todays date
$twoMonthsLater = strtotime("+3 months", $today); //3 months later
Now, you can easily compare them and determine.
I’d use PHP’s built-in DateTime and DateInterval classes for this.
<?php
// create a DateTime representation of your start date
// where $date is date in database
$resetDate = new DateTime($date);
// create a DateIntveral representation of 3 months
$passwordExpiry = new DateInterval('3M');
// add DateInterval to DateTime
$resetDate->add($passwordExpiry);
// compare $resetDate to today’s date
$difference = $resetDate->diff(new DateTime());
if ($difference->m > 3) {
// date is more than three months apart
}
I would do the date comparison in your SQL expression.
Otherwise, PHP has a host of functions that allow easy manipulation of date strings:
PHP: Date/Time Functions - Manual
I have a function which checks my database to see if a date exists, if it does exist, i want to display the next date which isnt in the database.
Is this possible?
My function returns 1 if there is a date in the database and 0 if there isnt, im using codeigniter, but not using any built in functions.
Its basically an availability checker, it allows us to input many different dates in the database, so calling my function i use
$availcheck = $ci->availability->check_availability_by_date(date('d/m/Y'));
The i use a if statement to check if the first time it runs it returns a value, this is how i have it
if($availcheck > 0){
// loop through the next dates and run the function again to see if it returns 0
} else {
echo 'available now';
}
I guess i would add 1 to the current date, check that one, then add another 1 and check that and so on.
Im just not sure how.
Cheers,
if i understand you correct , your problem is adding the day ?
if so i would suggest using the epoch or unix time
so convert the date to unix time using mktime than just add 1 day in seconds (24*60*60)
and then convert back to d/m/y format.
you can use the date function.
$date = time(); // get current timestamp
while ($availcheck) // while date IS found in database
{
$availcheck = $ci->availability->check_availability_by_date(date('d/m/Y',$date));
$date = $date + (24*60*60); // add one day
}
$date = $date - (24*60*60); // reduce one day
echo date('d/m/Y',$date); // prints the first date that is not in the DB
This SQL code could work for me.
$today = date("Y-m-d"); //today
$sql = "SELECT date FROM calendar WHERE date>'{$today}' AND date<='2100-12-31' AND date='0000-00-00' LIMIT 1";
Since you can't determine the ending date, 2100 could be for testing.
I have a week calendar that holds events, and want that users can't add events for the past days. So I'm tring to use a function like that:
if( strtotime($this->day) < time() ){ // date format is YYYY-MM-DD
// date is past
}else{
// date is not past
}
It seems to works fine, except that it consider today date as a past day. What am i doing wrong?
Simpler ->
if(strtotime($this->day) < strtotime(date('Y-m-d')))
{
...
}
else
{
...
}
A timestamp never contains only the date, but is always down to the current second. strtotime($this->day) is going to return today's date at 0:00, while you are comparing it against now, say, 11:12.
You could use strtotime("$this->day 12:59:59pm"); (if the format of $this->day allows for that) or use tomorrow's timestamp.
if(strtotime($this->day) < mktime(0, 0, 0)){
// date is past
} else {
// date is not past
}