This question already has answers here:
How to compare two Carbon Timestamps?
(4 answers)
Closed 5 years ago.
I wish to compare two dates - $dateX (datetime in past) with $currentDatetime (current datetime). For instance: check if $dateX was 7 days ago or not. All should happen in Laravel Blade Engine view by using Carbon.
Can you give some example? Thanks!
This think can be achieved easily with Carbon, so i'm supposing that both $dateX and $currentDateTime are Carbon instances, accordingly if you wanna check time diff in days simply use diffInDays
for example
if( $currentDateTime->diffInDays( $dateX ) > 7 ){
// do sonething here
}
in the end i really wanna say that carbon docs are very clear and easy to read
If you want to know if the date is earlier than a week ago, you could do this:
#if ($dateX < now()->subWeek())
The diffInDays() will work too, but only if all $dateX days are in the past and always will be. The code above is more explicit. Also, what if you'll need to change the logic?
First get these date times into carbon instances.
#if ($dateX->diffInDays($currentDateTime, false) == 7)
...
#endif
Carbon Docs - Difference
You can check this with diff. Also check if a date is set before the other.
$date1 = \Carbon\Carbon::create(2017, 10, 10);
$date2 = \Carbon\Carbon::create(2017, 10, 20);
$difference = $date1->diff($date2)->days;
$before = $date1 < $date2;
if (before && $difference < 7) {
//Date 1 more than 7 days before date 2
}
Related
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 years ago.
I have two dates. One of them is the current date, one of them is a date somebody uploaded something, this is stored in a database. I need to find out if the date stored in the database is older than 7 days from the current date. I'm using PHP's date(d/m/y);, I've tried some things online, I've tried dateDifference() from php.net, I've tried converting them to timestamps and taking them away, but neither of these seem to work. Is there a simpler way?
This is what carbon is made for. Consider it.
//time in db is in this format 2018-03-16 08:31:09 for this example
$dateInDb = Carbon::createFromFormat("Y-m-d H:i:s",$timeInDb);
$days = Carbon::now()->diffInDays($dateInDb);
Check the library here
try this:
<?php
$upload_date = '09/03/2018'; # d/m/Y format
if (strtotime(date_format(date_create_from_format('d/m/Y',$upload_date),'Y-m-d')) < strtotime('-7 days')) {
echo 'Upload date is older than 7 days ago.';
} else {
echo 'Upload date is not older than 7 days ago.';
}
I have a field called RegistrationDate which I need to check for time since.
I need a where claus that will do:
if (time() - strtotime($car->RegistrationDate) > 3*365.25*24*60*60)
over 3 years old
}
So:
$cars::where('RegistrationDate', '>', '3 years old')->get();
but don't know how to do that, my dates are saved as timestamps: 2009-07-17 00:00:00.
thanks.
Use the Carbon Library. I think it is loaded by the laravel framework already. But if not you can load via composer. https://github.com/briannesbitt/Carbon
$cars::where('RegistrationDate', '>', Carbon\Carbon::now()->subYears(3))->get(); Assuming $cars is an eloquent object for your Car model;
Also, unrelated to question... but your table naming conventions are a bit weird.
$cars = Car::where('registration_date', '>', Carbon\Carbon::now()->subYears(3))->get();
You can also use DateTime and DateTimeInterval class:
$currentDate = new DateTime(); // get current date time
$currentDate->sub(new DateInterval('P3Y')); // substract 3 years from current date
After that you can use $currentDate in your code like:
$cars::where('RegistrationDate', '>', $currentDate)->get();
You can also convert $currentDate to sql datetime string you needed with:
$currentDateStr = $currentDate->format('Y-m-d H:i:s');
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: Date larger than current date
I have my dates in this format: dd-mm-yyyy
Now according to php, 28-06-2011 is bigger than 01-11-2011
$today = date('d-m-Y', time());
if("28-06-2011" > $today ){
echo "This returns true";
}
How can i make this work properly?
You can't compare dates like that. They're two strings. PHP will try to interpret them as numbers (taking the first two digits and then failing because of the -) and compare those. Turns out this is not correct. YYYY-MM-DD type strings can indeed be compared using > and <.
So refer to Col. Shrapnel's answer and change your date format - it makes sense anyway. If you can't do that (because it's a user-input date or whatever), you can create two DateTime objects which, thanks to PHP 5 magic, you can also compare like this:
$date1 = new DateTime('2000-01-01');
$date2 = new DateTime('2001-01-01');
if ($date1 > $date2)... // this works
if(strtotime("28-06-2011") > time()) {
}
I have my dates in this format: dd-mm-yyyy
That is where you are wrong.
you shouldn't have your dates in this firmat. Have them in yyyy-mm-dd and you'll be able to compare them with no problem.
You cant compare date with string.
Create another date item similar to today variable with fixed date of 28-06-2011 and then compare 2 objects.
$datetocompare = date("d-m-Y", mktime(0, 0, 0, 28, 06, 2011));
if($datetocompare>$today) {}
this would then work.
Source: http://uk.php.net/manual/en/function.date.php
If you do it that way, you're actually comparing strings, rather than dates. To correctly compare dates, you would need to convert the date values/strings to time stamps, you can use the strtotime function to accomplish just that.
Here is a link with more info: http://php.net/manual/en/function.strtotime.php
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
I will ask the user to enter start date and end date. I would want to get all of the dates from start to end and get the number of days between those dates. How can I do this in PHP given start and end date.
When dealing with dates, you can convert to timestamps (seconds) as Ignacio recommends. But generally if possible you will be better off working with actual dates & days at a higher level.
For this, see the DateTime class built into PHP:
http://www.php.net/manual/en/class.datetime.php
http://www.php.net/manual/en/class.dateinterval.php
These are well supported in PHP 5.2, but PHP 5.3 adds even better DateTime handling functionality.
End result of this code will be number of days.
$days = (strtotime(date("Y-m-d"))-strtotime("2010-08-20")) / (60 * 60 * 24);
echo $days;
Read my answer to this topic, with examples:
Calculate number of hours between 2 dates in PHP
Depending on the input the user gives, I would most likely be using something like
$dateOne = (int)(mktime(0, 0, 0, $month1, $day1, $year1)/86400); //Get the first date as a unix timestamp, then convert to days.
$dateTwo = (int)(mktime(0, 0, 0, $month2, $day2, $year2)/86400); //Get the second date as a unix timestamp, then convert to days.
// Example
//$dateOne = (int)(mktime(0,0,0,12,03,2009)/86400);
//$dateTwo = (int)(mktime(0,0,0,08,19,2011)/86400);
$daysBetween = $dateTwo-$dateOne; //Calculate days between.
echo $daysBetween.'<br />'; //Echo the days between.
for ($i=1; $i=$daysBetween; $i++) { //Loop through every day between, echo the date of that day.
echo date('Y-m-d', $dateOne*86400+($i*86400)).'<br />';
}
The above code will give you all the days, including the first and last date. To get only the ones in between change:
for ($i=1; $i=$daysBetween; $i++) { //Loop through every day between, echo the date of that day.
to:
for ($i=2; $i<=$daysBetween; $i++) { //Loop through every day between, echo the date of that day.
Have tested, works well.
NOTE: the first date MUST be before the last date.
Cheers
Charlie
Convert both dates to timestamps, then count from one to the other, converting back to dates.
Is there a way to find out if a date falls within 7 days of the current date using PHP? If there is, would it also be possible to figure out how many days away that date is?
$date = strtotime('2010-11-28');
if (strtotime('-7 days') < $date && $date < strtotime('+7 days')) {
// yup
}
$difference = abs($date - time()) / 60 / 60 / 24;
Could be refined a bit if you care about edge cases, whole days and daylight-savings/leap seconds issues, but this should hopefully give you the right idea. Of course the Date class should be the preferred method to handle this, but it's only available in PHP 5.3+.
For the first one, add 7 days to the current date and see which is greater.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
For the second one:
http://php.net/manual/en/function.date-diff.php