In php how to check if one date (given as string) differs to another date at least three month (unable to find any examples):
$date1 = "2013-11-05";
$date2 = "2013-11-19";
//both dates is in form yyyy.mm.dd
differsThreeMonths($date1,$date2) { ???? return $differs; }
differsThreeMonths("2013-11-05","2014-05-02");//true
differsThreeMonths("2014-01-01","2014-04-01");//true
differsThreeMonths("2014-01-01","2014-03-31");//false
differsThreeMonths("2013-12-01","2014-01-15");//false
etc
Thank you
$datetime1 = new DateTime('2013-07-01 12:00:00');
$datetime2 = new DateTime('2013-11-11 12:00:00');
$interval = $datetime1->diff($datetime2);
if(($interval->m>=3) || ($interval->y > 0))
echo "at least 3 months";
else
echo "Not greater than 3 months";
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
var_dump($interaval);
http://www.php.net/manual/en/datetime.diff.php
Try:
function differsThreeMonths($date_str1, $date_str2) {
if (strlen($date_str1) > 0 && strlen($date_str2) > 0) {
$date1 = new DateTime($date_str1);
$date2 = new DateTime($date_str2);
$since_start = $date1->diff($date2);
return (($since_start->y > 0) || ($since_start->m >= 3));
} else {
return -1; // ERROR CODE HERE
}
}
Related
I want to compare the interval of two datetimes to see if the interval is in the past, in the future or now.
$current_time = new DateTime();
$datetime1 = new DateTime('2018-03-17 18:25:00');
$datetime2 = new DateTime('2018-03-17 20:00:00');
if($current_time >= $datetime1 && $current_time <= $datetime2){
// now
} elseif($current_time >= $datetime1){
// past
} elseif($current_time <= $datetime1){
// future
}
EDIT:
Sorry, just realised posting my whole real code would make it easier for everyone.
The example above does work but it doesnt work when I loop thru the db using more than one interval from there
function interval(){
....
while($row = $result->fetch_assoc()){
$start_time = $row['start_time'];
$end_time = $row['end_time'];
$now = new DateTime();
$datetime1 = new DateTime($start_time);
$datetime2 = new DateTime($end_time);
if($now >= $datetime1 && $now <= $datetime2){
// now
}elseif($now < $datetime1 && $now < $datetime2){
// past
}elseif($now > $datetime1 && $now > $datetime2){
// future
}else{
// fail?
}
}
}
date returns a string. If you actually want to use a real DateTime object, then you need to do something like:
$now = new DateTime();
$other = new DateTime('2018-03-18 17:45:00'); //note: example made off the top of my head... value presented may not work
if ($now < $other) {
//do something
}
More information can, as always, be found in the PHP manual: https://secure.php.net/manual/en/class.datetime.php
As your dates are already string just compare them like that
$current_time = date("Y-m-d H:i:s");
$datetime1 = '2018-03-17 18:25:00';
$datetime2 = '2018-03-17 20:00:00';
if($current_time >= $datetime1 && $current_time <= $datetime2){
// now
} elseif($current_time >= $datetime1){
// past
} elseif($current_time <= $datetime1){
// future
}
Can you expand why your code isn't working as intended?
Below is how I would approach the question.
date_default_timezone_set('UTC');
$current_time = new DateTime();
$compareWith = new DateTime('2018-03-16 18:25:00');
if ($current_time >= $compareWith) {
//whatever your heart desires
}
else{
//whatever your heart desires
}
I have a date
2016-09-16
How can I check that if that date is less than 3 days old?
I'm being really stupid and the frustration is making me not figure it out
Here's my code
public function isNew()
{
return strtotime($this->created_at) > time() && strtotime($this->created_at) < strtotime('+3 days',time());
}
Should be easy to use DateTime and DateInterval to handle this.
$date = new DateTime('2016-09-16');
$diff = (new DateTime)->diff($date)->days;
return $diff < 3;
Using PHP's DateTime and DateInterval Classes would do you much good. Here's how:
<?php
function isNewerThan3Days($date) {
$today = new DateTime();
$date = new DateTime($date);
$diff = $today->diff($date);
$diffD = $diff->days;
if($diffD>=3){
return false;
}
return true;
}
var_dump(isNewerThan3Days("2016-09-14")); //<== YIELDS:: boolean true
Use diff(). It will return a DateInterval object. Within that object will be a days property (days) that you can access.
// compare two distinct dates:
$datetime1 = new DateTime('2016-09-16');
$datetime2 = new DateTime('2016-09-12');
$interval = $datetime1->diff($datetime2);
$daysOld = $interval->days;
// int(4)
// or compare today vs your date...
$datetime1 = new DateTime('2016-09-16');
$now = new DateTime();
$interval = $now->diff($datetime1);
$daysOld = $interval->days;
// int(0)
// then determine if it's at least 3 days old:
$is3daysOld = ($daysOld >= 3 ? true : false);
http://php.net/manual/en/datetime.diff.php
This should do it for you.
$date = new DateTime('2015-09-16');
$now = new DateTime();
$interval = $date->diff($now);
$difference = $interval->format('%a');
if($difference < 3) {
// $date is fewer than 3 days ago
}
In your isNew() method:
public function isNew() {
$created_at = new DateTime($this->created_at);
$now = new DateTime();
$interval = $created_at->diff($now);
$difference = $interval->format('%a');
if($difference < 3) {
return true;
}
return false;
}
return $this->created_at->diffInDays() < 3;
Without parameter diffInDays() will return the number of complete days between created_at and now.
Why don't you use Carbon ?
You can easily do that and many more in carbon like so :
$dt = Carbon::createFromDate(2011, 8, 1);
echo $dt->subDays(5)->diffForHumans(); // 5 days before
if
$_POST['SelectedDate1'] = 2013/08/05
and
$_POST['SelectedDate2'] = 2013/08/07
How can I set a variable which gives me back the number of days (2 in this case) to then echo it as result
I'm looking for a solution that can cover any calendar combination.
Is there any global function in php.
I think, in the following Documentation on PHP.net is exactly what you're trying to do.
http://nl3.php.net/manual/en/datetime.diff.php
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
In your case:
<?php
$first = new DateTime($_POST['SelectedDate1']);
$second = new DateTime($_POST['SelectedDate2']);
$passed = $first->diff($second);
var_dump($passed->format('%R%a days'));
For more formats, next to %R%a, see: http://nl3.php.net/manual/en/function.date.php
<?php
$days = (strtotime($_POST['SelectedDate2']) - strtotime($_POST['SelectedDate1'])) / 86400;
example:
<?php
$_POST['SelectedDate1'] = '2013/08/05' ;
$_POST['SelectedDate2'] = '2013/08/07' ;
$days = (strtotime($_POST['SelectedDate2']) - strtotime($_POST['SelectedDate1'])) / 86400;
var_export($days);
// output: 2
I use this function that I've found on this forum but I don't remember where :
function createDateRangeArray($start, $end) {
// Modified by JJ Geewax
$range = array();
if (is_string($start) === true) $start = strtotime($start);
if (is_string($end) === true ) $end = strtotime($end);
if ($start > $end) return createDateRangeArray($end, $start);
do {
$range[] = date('Y-m-d', $start);
$start = strtotime("+ 1 day", $start);
}
while($start <= $end);
return $range;
}
it returns a range of date as an array, then you just have to get the count
DateTime class is created for this:
$date1 = new DateTime('2013/08/05');
$date2 = new DateTime('2013/08/07');
$diff = $date1->diff($date2);
echo $diff->days;
I have a date list like:
2012-04-11
2012-04-29
2012-04-26
2012-04-23
2012-03-21
2012-07-23
2012-12-19
I want to compare date list with today date. From that I want a list of dates which have already past. Also, I want a list of dates which are in the Future.
You can use strtotime() to convert your dates to UNIX timestamp then perform a simple greater/less than test. Do something like this:
<?php
$past_dates = array();
$future_dates = array();
$dates = array('2012-04-11', '2012-04-29', '2012-04-26', '2012-04-23', '2012-03-21', '2012-07-23', '2012-12-19');
$today = date('Y-m-d');
foreach($dates as $value) {
if(strtotime($value) < strtotime($today)) {
$past_dates[] = $value;
} else if(strtotime($value) > strtotime($today)) {
$future_dates[] = $value;
}
}
echo 'Past dates:';
echo '<pre>';
print_r($past_dates);
echo 'Future dates:';
echo '<pre>';
print_r($future_dates);
echo '</pre>';
echo 'Today is: ' . $today;
?>
Use PHP's strtotime() method:
$date = "2012-04-29";
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$test_date = strtotime($date);
if ($test_date > $today) {
// Some Code
}
else {
// Some code
}
$dateArray = array('2012-04-11','2012-04-29','2012-04-26','2012-04-23','2012-03-21','2012-07-23','2012-12-19')
$pastDates = array();
$futureDates = array();
foreach ($dateArray as $date){
$dateTime = strtotime($date);
if (time() > $dateTime){
$pastDates[] = $date;
} else {
$futureDates[] = $date;
}
}
Try http://www.php.net/manual/en/datetime.diff.php
From manual:
Object oriented style
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
Procedural style
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
The above examples will output:
+2 days
Also maybe you will find usefull.
Example #2 DateTime object comparison
Note:
As of PHP 5.2.2, DateTime objects can be compared using comparison operators.
<?php
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 == $date2);
var_dump($date1 < $date2);
var_dump($date1 > $date2);
?>
The above example will output:
bool(false)
bool(true)
bool(false)
I'm trying to use date_diff():
$datetime1 = date_create('19.03.2010');
$datetime2 = date_create('22.04.2010');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');
Its doesn't work for me, gives an error:
Call to undefined function date_diff()
How can I get it work?
PHP 5.2 is used.
Thanks.
The function date_diff requires a PHP version of 5.3 or greater.
UPDATE
An example for PHP 5.2 (taken from the date_diff user comments).
<?php
function date_diff($date1, $date2) {
$current = $date1;
$datetime2 = date_create($date2);
$count = 0;
while(date_create($current) < $datetime2){
$current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current)));
$count++;
}
return $count;
}
echo (date_diff('2010-3-9', '2011-4-10')." days <br \>");
?>
Here is a version that doesn't use Date objects, but these are of no use anyways in 5.2.
function date_diff($d1, $d2){
$d1 = (is_string($d1) ? strtotime($d1) : $d1);
$d2 = (is_string($d2) ? strtotime($d2) : $d2);
$diff_secs = abs($d1 - $d2);
return floor($diff_secs / (3600 * 24));
}
First convert both dates to mm/dd/yyyy format then do this :
$DateDiff = floor( strtotime($datetime2 ) - strtotime( $datetime1 ) ) / 86400 ;
//this will yield the resultant difference in days
function date_diff($date1, $date2) {
$count = 0;
//Ex 2012-10-01 and 2012-10-20
if(strtotime($date1) < strtotime($date2))
{
$current = $date1;
while(strtotime($current) < strtotime($date2)){
$current = date("Y-m-d",strtotime("+1 day", strtotime($current)));
$count++;
}
}
//Ex 2012-10-20 and 2012-10-01
else if(strtotime($date2) < strtotime($date1))
{
$current = $date2;
while(strtotime($current) < strtotime($date1)){
$current = date("Y-m-d",strtotime("+1 day", strtotime($current)));
$count++;
}
$current = $current * -1;
}
return $count; }
Converting your DateTime to Unix date type, and subtracting one from another:
The format->("U") is where the DateTime is converted.
$datetime1 = date_create('19.03.2010');
$datetime2 = date_create('22.04.2010');
$intervalInDays = ($datetime2->format("U") - $datetime1->format("U"))/(3600 * 24);
Not sure if this is Y2K38 safe but it's one of the simplest date_diff workarounds.