I need to find the maximum and minimum date from a given array using PHP.
I have $date_arr which contains following values,
$date_arr = array('0'=>'20-05-2015','1'=>'02-01-2015','2'=>'30-03-2015');
Here, I need to get the larger date as '20-05-2015' and the minimum date as '02-01-2015'.
How can I achieve this?
max() and min() works fine with your array:
echo "Latest Date: ". max($dates)."\n";
echo "Earliest Date: ". min($dates)."\n";
<?php
$date_arr=array(0=>'20-05-2015',1=>'02-01-2015',2=>'30-03-2015');
usort($date_arr, function($a, $b) {
$dateTimestamp1 = strtotime($a);
$dateTimestamp2 = strtotime($b);
return $dateTimestamp1 < $dateTimestamp2 ? -1: 1;
});
echo 'Min: ' . $date_arr[0];
echo '<br/>';
echo 'Max: ' . $date_arr[count($date_arr) - 1];
?>
please Try this
$date_arr = array('0' => '20-05-2015', '1' => '02-01-2015', '2' => '30-03-2015');
for ($i = 0; $i < count($date_arr); $i++)
{
if ($i == 0)
{
$max_date = date('Y-m-d H:i:s', strtotime($date_arr[$i]));
$min_date = date('Y-m-d H:i:s', strtotime($date_arr[$i]));
}
else if ($i != 0)
{
$new_date = date('Y-m-d H:i:s', strtotime($date_arr[$i]));
if ($new_date > $max_date)
{
$max_date = $new_date;
}
else if ($new_date < $min_date)
{
$min_date = $new_date;
}
}
}
echo date('d-m-Y',strtotime($max_date));
echo date('d-m-Y',strtotime($min_date));
Thought it doesn't technically offer the lowest computational time complexity, array_multisort() is a sensible, readable, concise approach. My snippet only calls strtotime() on each element once -- usort() cannot match this claim.
Code: (Demo)
$dates = ['20-05-2015', '02-01-2015', '30-03-2015', '10-01-1990'];
array_multisort(array_map('strtotime', $dates), $dates);
printf(
"Latest Date: %s\nEarliest Date: %s",
$dates[array_key_last($dates)],
$dates[0]
);
Output:
Latest Date: 10-01-1990
Earliest Date: 20-05-2015
To arrive at the same result by calling min() and max(), just create a formatted copy of the dates in unix time.
Code: (Demo)
$unix = array_map('strtotime', $dates);
printf(
"Latest Date: %s\nEarliest Date: %s",
date('d-m-Y', max($unix)),
date('d-m-Y', min($unix))
);
If you want to use usort(), here is the most modern syntax with the spaceship operator and arrow function syntax. (Demo)
usort($dates, fn($a, $b) => strtotime($a) <=> strtotime($b));
printf(
"Latest Date: %s\nEarliest Date: %s",
$dates[array_key_last($dates)],
$dates[0]
);
This task can surely be accomplished tens of different ways. I considered writing a foreach() loop with conditions to maintain temporary variables while making iterated comparisons, but I felt it was prohibitively convoluted for a rather simple task.
$date_arr=array(0=>'2015-05-20',1=>'2015-02-21',2=>'2015-04-13',3=>'2020-04-30',4=>'2020-04-13');
$max_date=$date_arr[0];
for($i=0;$i<count($date_arr);$i++)
{
echo $date_arr[$i]. ' ,';
if( $max_date < $date_arr[$i+1])
{
$max_date=$date_arr[$i+1];
}
}
echo " Max= ". $max_date;
Related
This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 1 year ago.
How to compare two dates in php if dates are in format '03_01_12' and '31_12_11' .
I am using this code:
$date1=date('d_m_y');
$date2='31_12_11';
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
But its not working..
You will have to make sure that your dates are valid date objects.
Try this:
$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
You can then perform the strtotime() method to get the difference.
Using DateTime::createFromFormat:
$format = "d_m_y";
$date1 = \DateTime::createFromFormat($format, "03_01_12");
$date2 = \DateTime::createFromFormat($format, "31_12_11");
var_dump($date1 > $date2);
Not answering the OPs actual problem, but answering just the title. Since this is the top result for "comparing dates in php".
Pretty simple to use Datetime Objects (php >= 5.3.0) and Compare them directly
$date1 = new DateTime("2009-10-11");
$date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
var_dump($date1 < $date2);
The date_diff() function returns the difference between two DateTime objects.
If the first date is before the second date a positive number of days will be returned; otherwise a negative number of days:
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
output will be "+272 days" ;
changing
$date1 = "2014-03-15"
<?php
$date1=date_create("2014-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
Output will be "-93 days"
<?php
$expiry_date = "2017-12-31 00:00:00"
$today = date('d-m-Y',time());
$exp = date('d-m-Y',strtotime($expiry_date));
$expDate = date_create($exp);
$todayDate = date_create($today);
$diff = date_diff($todayDate, $expDate);
if($diff->format("%R%a")>0){
echo "active";
}else{
echo "inactive";
}
echo "Remaining Days ".$diff->format("%R%a days");
?>
Extending #nevermind's answer, one can use DateTime::createFromFormat: like,
// use - instead of _. replace _ by - if needed.
$format = "d-m-y";
$date1 = DateTime::createFromFormat($format, date('d-m-y'));
$date2 = DateTime::createFromFormat($format, str_replace("_", "-",$date2));
var_dump($date1 > $date2);
you can try something like:
$date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
$date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
$dateDiff = date_diff($date1, $date2);
var_dump($dateDiff);
You can then access the difference in days like this $dateDiff->d;
Try this
$data1 = strtotime(\date("d/m/Y"));
$data1 = date_create($data1);
$data2 = date_create("21/06/2017");
if($data1 < $data2){
return "The most current date is date1";
}
return "The most current date is date2";
You can to converte for integer number and compare.
Eg.:
$date_1 = date('Ymd');
$date_2 = '31_12_2011';
$date_2 = (int) implode(array_reverse(explode("_", $date_2)));
echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';
benchmark comparison
date_create, strtotime, DateTime, and direct
direct is faster
$f1="2014-12-12";
$f2="2014-12-13";
$diff=false;
$this->bench('a');
for ($i=0; $i < 20000; $i++) {
$date1=date_create($f1);
$date2=date_create($f2);
$diff=date_diff($date1,$date2);
}
$this->bench('a','b');
for ($i=0; $i < 20000; $i++) {
$date1=strtotime($f1);
$date2=strtotime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$this->bench('b','c');
for ($i=0; $i < 20000; $i++) {
$date1 = new DateTime($f1);
$date2 = new DateTime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$diff=false;
$this->bench('c','d');
for ($i=0; $i < 20000; $i++) {
if ($f1>$f2) {
$diff=true;
}
}
$this->bench('d','e');
var_dump($diff);
$this->dumpr($this->benchs);
results:
[a] => 1610415241.4687
[b] => 1610415242.8759
[a-b] => 1.407194852829
[c] => 1610415243.5672
[b-c] => 0.69137716293335
[d] => 1610415244.7036
[c-d] => 1.1363649368286
[e] => 1610415244.7109
[d-e] => 0.0073208808898926
compare the result of maketime() for each of the time
I know this is late, but for future reference, put the date format into a recognised format by using str_replace then your function will work. (replace the underscore with a dash)
//change the format to dashes instead of underscores, then get the timestamp
$date1 = strtotime(str_replace("_", "-",$date1));
$date2 = strtotime(str_replace("_", "-",$date2));
//compare the dates
if($date1 < $date2){
//convert the date back to underscore format if needed when printing it out.
echo '1 is small='.$date1.','.date('d_m_y',$date1);
}else{
echo '2 is small='.$date2.','.date('d_m_y',$date2);
}
Don't know what your problem is but:
function date_compare($d1, $d2)
{
$d1 = explode('_', $d1);
$d2 = explode('_', $d2);
$d1 = array_reverse($d1);
$d2 = array_reverse($d2);
if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
{
return $d2;
}
else
{
return $d1;
}
}
I think this one is very simple function
function terminateOrNotStringtoDate($currentDate, $terminationdate)
{
$crtDate = new DateTime($currentDate);
$termDate = new DateTime($terminationdate);
if($crtDate >= $termDate)
{
return true;
} else {
return false;
}
}
Guys Please don't make it so complex The simple answer bellow
$date1=date('d_m_y');
$date2='31_12_11';
$date1=str_replace('_', '-', $date1);
$date2=str_replace('_', '-', $date2)
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
I just have added two more lines with your code
If both dates are in the same format then use a comparison operator.
$date1 = "2018-05-05";
$date2 = "2019-08-19";
//comparison operator to
if ($date1 > $date2) {
echo "$date1 is latest than $date2";
}
else{
echo "$date1 is older than $date2";
}
Output:
2018-05-05 is older than 2019-08-19
This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 1 year ago.
How to compare two dates in php if dates are in format '03_01_12' and '31_12_11' .
I am using this code:
$date1=date('d_m_y');
$date2='31_12_11';
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
But its not working..
You will have to make sure that your dates are valid date objects.
Try this:
$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
You can then perform the strtotime() method to get the difference.
Using DateTime::createFromFormat:
$format = "d_m_y";
$date1 = \DateTime::createFromFormat($format, "03_01_12");
$date2 = \DateTime::createFromFormat($format, "31_12_11");
var_dump($date1 > $date2);
Not answering the OPs actual problem, but answering just the title. Since this is the top result for "comparing dates in php".
Pretty simple to use Datetime Objects (php >= 5.3.0) and Compare them directly
$date1 = new DateTime("2009-10-11");
$date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
var_dump($date1 < $date2);
The date_diff() function returns the difference between two DateTime objects.
If the first date is before the second date a positive number of days will be returned; otherwise a negative number of days:
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
output will be "+272 days" ;
changing
$date1 = "2014-03-15"
<?php
$date1=date_create("2014-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
Output will be "-93 days"
<?php
$expiry_date = "2017-12-31 00:00:00"
$today = date('d-m-Y',time());
$exp = date('d-m-Y',strtotime($expiry_date));
$expDate = date_create($exp);
$todayDate = date_create($today);
$diff = date_diff($todayDate, $expDate);
if($diff->format("%R%a")>0){
echo "active";
}else{
echo "inactive";
}
echo "Remaining Days ".$diff->format("%R%a days");
?>
Extending #nevermind's answer, one can use DateTime::createFromFormat: like,
// use - instead of _. replace _ by - if needed.
$format = "d-m-y";
$date1 = DateTime::createFromFormat($format, date('d-m-y'));
$date2 = DateTime::createFromFormat($format, str_replace("_", "-",$date2));
var_dump($date1 > $date2);
you can try something like:
$date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
$date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
$dateDiff = date_diff($date1, $date2);
var_dump($dateDiff);
You can then access the difference in days like this $dateDiff->d;
Try this
$data1 = strtotime(\date("d/m/Y"));
$data1 = date_create($data1);
$data2 = date_create("21/06/2017");
if($data1 < $data2){
return "The most current date is date1";
}
return "The most current date is date2";
You can to converte for integer number and compare.
Eg.:
$date_1 = date('Ymd');
$date_2 = '31_12_2011';
$date_2 = (int) implode(array_reverse(explode("_", $date_2)));
echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';
benchmark comparison
date_create, strtotime, DateTime, and direct
direct is faster
$f1="2014-12-12";
$f2="2014-12-13";
$diff=false;
$this->bench('a');
for ($i=0; $i < 20000; $i++) {
$date1=date_create($f1);
$date2=date_create($f2);
$diff=date_diff($date1,$date2);
}
$this->bench('a','b');
for ($i=0; $i < 20000; $i++) {
$date1=strtotime($f1);
$date2=strtotime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$this->bench('b','c');
for ($i=0; $i < 20000; $i++) {
$date1 = new DateTime($f1);
$date2 = new DateTime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$diff=false;
$this->bench('c','d');
for ($i=0; $i < 20000; $i++) {
if ($f1>$f2) {
$diff=true;
}
}
$this->bench('d','e');
var_dump($diff);
$this->dumpr($this->benchs);
results:
[a] => 1610415241.4687
[b] => 1610415242.8759
[a-b] => 1.407194852829
[c] => 1610415243.5672
[b-c] => 0.69137716293335
[d] => 1610415244.7036
[c-d] => 1.1363649368286
[e] => 1610415244.7109
[d-e] => 0.0073208808898926
compare the result of maketime() for each of the time
I know this is late, but for future reference, put the date format into a recognised format by using str_replace then your function will work. (replace the underscore with a dash)
//change the format to dashes instead of underscores, then get the timestamp
$date1 = strtotime(str_replace("_", "-",$date1));
$date2 = strtotime(str_replace("_", "-",$date2));
//compare the dates
if($date1 < $date2){
//convert the date back to underscore format if needed when printing it out.
echo '1 is small='.$date1.','.date('d_m_y',$date1);
}else{
echo '2 is small='.$date2.','.date('d_m_y',$date2);
}
Don't know what your problem is but:
function date_compare($d1, $d2)
{
$d1 = explode('_', $d1);
$d2 = explode('_', $d2);
$d1 = array_reverse($d1);
$d2 = array_reverse($d2);
if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
{
return $d2;
}
else
{
return $d1;
}
}
I think this one is very simple function
function terminateOrNotStringtoDate($currentDate, $terminationdate)
{
$crtDate = new DateTime($currentDate);
$termDate = new DateTime($terminationdate);
if($crtDate >= $termDate)
{
return true;
} else {
return false;
}
}
Guys Please don't make it so complex The simple answer bellow
$date1=date('d_m_y');
$date2='31_12_11';
$date1=str_replace('_', '-', $date1);
$date2=str_replace('_', '-', $date2)
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
I just have added two more lines with your code
If both dates are in the same format then use a comparison operator.
$date1 = "2018-05-05";
$date2 = "2019-08-19";
//comparison operator to
if ($date1 > $date2) {
echo "$date1 is latest than $date2";
}
else{
echo "$date1 is older than $date2";
}
Output:
2018-05-05 is older than 2019-08-19
This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 1 year ago.
How to compare two dates in php if dates are in format '03_01_12' and '31_12_11' .
I am using this code:
$date1=date('d_m_y');
$date2='31_12_11';
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
But its not working..
You will have to make sure that your dates are valid date objects.
Try this:
$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
You can then perform the strtotime() method to get the difference.
Using DateTime::createFromFormat:
$format = "d_m_y";
$date1 = \DateTime::createFromFormat($format, "03_01_12");
$date2 = \DateTime::createFromFormat($format, "31_12_11");
var_dump($date1 > $date2);
Not answering the OPs actual problem, but answering just the title. Since this is the top result for "comparing dates in php".
Pretty simple to use Datetime Objects (php >= 5.3.0) and Compare them directly
$date1 = new DateTime("2009-10-11");
$date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
var_dump($date1 < $date2);
The date_diff() function returns the difference between two DateTime objects.
If the first date is before the second date a positive number of days will be returned; otherwise a negative number of days:
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
output will be "+272 days" ;
changing
$date1 = "2014-03-15"
<?php
$date1=date_create("2014-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
Output will be "-93 days"
<?php
$expiry_date = "2017-12-31 00:00:00"
$today = date('d-m-Y',time());
$exp = date('d-m-Y',strtotime($expiry_date));
$expDate = date_create($exp);
$todayDate = date_create($today);
$diff = date_diff($todayDate, $expDate);
if($diff->format("%R%a")>0){
echo "active";
}else{
echo "inactive";
}
echo "Remaining Days ".$diff->format("%R%a days");
?>
Extending #nevermind's answer, one can use DateTime::createFromFormat: like,
// use - instead of _. replace _ by - if needed.
$format = "d-m-y";
$date1 = DateTime::createFromFormat($format, date('d-m-y'));
$date2 = DateTime::createFromFormat($format, str_replace("_", "-",$date2));
var_dump($date1 > $date2);
you can try something like:
$date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
$date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
$dateDiff = date_diff($date1, $date2);
var_dump($dateDiff);
You can then access the difference in days like this $dateDiff->d;
Try this
$data1 = strtotime(\date("d/m/Y"));
$data1 = date_create($data1);
$data2 = date_create("21/06/2017");
if($data1 < $data2){
return "The most current date is date1";
}
return "The most current date is date2";
You can to converte for integer number and compare.
Eg.:
$date_1 = date('Ymd');
$date_2 = '31_12_2011';
$date_2 = (int) implode(array_reverse(explode("_", $date_2)));
echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';
benchmark comparison
date_create, strtotime, DateTime, and direct
direct is faster
$f1="2014-12-12";
$f2="2014-12-13";
$diff=false;
$this->bench('a');
for ($i=0; $i < 20000; $i++) {
$date1=date_create($f1);
$date2=date_create($f2);
$diff=date_diff($date1,$date2);
}
$this->bench('a','b');
for ($i=0; $i < 20000; $i++) {
$date1=strtotime($f1);
$date2=strtotime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$this->bench('b','c');
for ($i=0; $i < 20000; $i++) {
$date1 = new DateTime($f1);
$date2 = new DateTime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$diff=false;
$this->bench('c','d');
for ($i=0; $i < 20000; $i++) {
if ($f1>$f2) {
$diff=true;
}
}
$this->bench('d','e');
var_dump($diff);
$this->dumpr($this->benchs);
results:
[a] => 1610415241.4687
[b] => 1610415242.8759
[a-b] => 1.407194852829
[c] => 1610415243.5672
[b-c] => 0.69137716293335
[d] => 1610415244.7036
[c-d] => 1.1363649368286
[e] => 1610415244.7109
[d-e] => 0.0073208808898926
compare the result of maketime() for each of the time
I know this is late, but for future reference, put the date format into a recognised format by using str_replace then your function will work. (replace the underscore with a dash)
//change the format to dashes instead of underscores, then get the timestamp
$date1 = strtotime(str_replace("_", "-",$date1));
$date2 = strtotime(str_replace("_", "-",$date2));
//compare the dates
if($date1 < $date2){
//convert the date back to underscore format if needed when printing it out.
echo '1 is small='.$date1.','.date('d_m_y',$date1);
}else{
echo '2 is small='.$date2.','.date('d_m_y',$date2);
}
Don't know what your problem is but:
function date_compare($d1, $d2)
{
$d1 = explode('_', $d1);
$d2 = explode('_', $d2);
$d1 = array_reverse($d1);
$d2 = array_reverse($d2);
if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
{
return $d2;
}
else
{
return $d1;
}
}
I think this one is very simple function
function terminateOrNotStringtoDate($currentDate, $terminationdate)
{
$crtDate = new DateTime($currentDate);
$termDate = new DateTime($terminationdate);
if($crtDate >= $termDate)
{
return true;
} else {
return false;
}
}
Guys Please don't make it so complex The simple answer bellow
$date1=date('d_m_y');
$date2='31_12_11';
$date1=str_replace('_', '-', $date1);
$date2=str_replace('_', '-', $date2)
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
I just have added two more lines with your code
If both dates are in the same format then use a comparison operator.
$date1 = "2018-05-05";
$date2 = "2019-08-19";
//comparison operator to
if ($date1 > $date2) {
echo "$date1 is latest than $date2";
}
else{
echo "$date1 is older than $date2";
}
Output:
2018-05-05 is older than 2019-08-19
I want to only shows result for the current month, but I have no idea how to do this, my current output is like this.
Output
2014-04-11
-> array
2014-04-11
2014-04-05
2014-03-29
PHP
$date = date('Y-m-d');
echo $date;
echo "<pre>";
foreach ($submissions as $test){
if($date >= substr($test['thing']['created'], 0, 10)){
echo substr($test['thing']['created'], 0, 10);
echo "<br>";
}
}
echo "</pre>";
My current code wont work as its only checking if the whole number is greater or equal to, any ideas anyone?
Just try with strtotime:
$year = date('Y');
$month = date('m');
foreach ($submissions as $test) {
$timestamp = strtotime($test['thing']['created']);
$testYear = date('Y', $timestamp);
$testMonth = date('m', $timestamp);
if (($month >= $testMonth && $year == $testYear) || $year > $testYear) {
// test passed
}
}
If your problem is to have only results of current month :
$date = date('Y-m');
echo "<pre>";
foreach ($submissions as $test){
// if month & year is equal
if($date == substr($test['thing']['created'], 0, 7)){
echo substr($test['thing']['created'], 0, 10);
echo "<br>";
}
}
echo "</pre>";
If you want to sort it, you will have to convert to timestamp
i'm not a pro in php but take a look here : http://fr2.php.net/manual/fr/function.date.php
does date('m') would be a solution for your problem?
You can try 2 ways :
convert them to timestamp, then compare the int
If you are taking the dates frome a DB, ORDER BY date in the query
Variable for current month first $cur_m=date('m')
Explode your variable in loop with -.
$p = explode('-',$test['thing']['created']);
in loop check for current month like below
if($p[1]==$cur_m){ your code... }
I've been reading about problems in php with strtotime and "next month" issues. What i want to make is counter of months between two dates.
For example if I have start date 01.02.2012 and stop date 07.04.2012 I'd like to get return value - 3 months. Also 3 months would be the result if start date i 28.02.2012 and 07.04.2012. I am not counting exact number of days/months, just a number of months I have between two dates. It's not a big deal to make it with some strange date, mktime and strtotime usage, but unfortunatelly start and stop dates might be in two different years so
mktime(0,0,0,date('m')+1,1,date('Y');
isnt going to work (i do not now the year and if it changes between start and stop date. i can calculate it but it is not nice solution). Perfect solution would be to use:
$stat = Array('02.01.2012', '07.04.2012')
$cursor = strtotime($stat[0]);
$stop = strtotime($stat[1]);
$counter = 0;
while ( $cursor < $stop ) {
$cursor = strtotime("first day of next month", $cursor);
echo $cursor . '<br>';
$counter++;
if ( $counter > 100) { break; } // safety break;
}
echo $counter . '<br>';
Unfortunatelly strtotime isnt returning proper values. If I use it is returning empty string.
Any ideas how to get timestamp of the first day of next month?
SOLUTION
$stat = Array('02.01.2012', '01.04.2012');
$start = new DateTime( $stat[0] );
$stop = new DateTime( $stat[1] );
while ( $start->format( 'U') <= $stop->format( 'U' ) ) {
$counter ++;
echo $start->format('d:m:Y') . '<br>';
$start->modify( 'first day of next month' );
}
echo '::' . $counter . '..<br>';
<?php
$stat = Array('02.01.2012', '07.04.2012');
$stop = strtotime($stat[1]);
list($d, $m, $y) = explode('.', $stat[0]);
$count = 0;
while (true) {
$m++;
$cursor = mktime(0, 0, 0, $m, $d, $y);
if ($cursor < $stop) $count ++; else exit;
}
echo $count;
?>
the easy way :D