Comparing day and month in php - php

Good evening!
Problem:
The problem is to compare 2 dates in PHP.
I want to only compare day and month, excluding the year.
I want the code to first check the month, if the month is same or less than current month. If it's true, move on to check the day. If the day is equal to, or less than current day, execute a custom code.
What I've tried:
Here is where I got so far -
<?php
$oldDate = "26/02/1815";
$latestDate = explode("/", $oldDate);
$year = $latestDate[2];
$month = $latestDate[1];
$day = $latestDate[0];
$newDate = $month.'/'.$day.'/'.$year;
$nowDate = date('m/d/Y');
$nownowDate = explode("/", $nowDate);
$nowYear = $nownowDate[2];
$nowMonth = $nownowDate[0];
$nowDay = $nownowDate[1];
if ($nowMonth <= $month) {
if ($nowDay <= $day) {
echo<<<NEXTDATE
<li class="next"><?php echo link_to_next_item_show(); ?></li> //This is the custom code
NEXTDATE;
}
}
?>
I feel that there is something wrong with my IFs statement.

From:
Elegant way to get the count of months between two dates?
$timezone = new DateTimeZone('America/New_York');
$d1 = new DateTime("1815-02-26", $timezone);
$d2 = new DateTime("2015-01-01", $timezone);
var_dump($d1->diff($d2)->m); // int(4)
var_dump($d1->diff($d2)->d); // int(4)
if(($d1->diff($d2)->m) && ($d1->diff($d2)->d)){
echo "run code here";
}

You cannot put <?php tags inside a heredoc. So, instead of using a heredoc you could do:
echo '<li class="next">' . link_to_next_item_show() . '</li>';

Thank you all for trying your best at solving this problem. Maybe I didn't make it clear, that's why some of you were struggling, trying to understand what I was talking about.
I've tried to solve this problem using switch, so here is the answer that works for me.
<?php
$oldDate = metadata('item', array('Dublin Core', 'Date'));
$latestDate = explode("/", $oldDate);
$year = $latestDate[2];
$month = $latestDate[1];
$day = $latestDate[0];
$newDate = $month.'/'.$day.'/'.$year;
$nowDate = date('m/d/Y');
$nownowDate = explode("/", $nowDate);
$nowYear = $nownowDate[2];
$nowMonth = $nownowDate[0];
$nowDay = $nownowDate[1];
switch (true):
case ($month == $nowMonth):
if ($day < $nowDay) {
echo '<li class="next">' . link_to_next_item_show() . '</li>';
} else {
echo " ";
}
break;
case ($month < $nowMonth):
echo '<li class="next">' . link_to_next_item_show() . '</li>';
break;
case ($month > $nowMonth):
echo " ";
break;
default :
echo " ";
break;
endswitch;
?>
Thank you to #developerwjk for the correction on using php tag inside heredoc. Now I know what's wrong with the code. I'm providing this as an answer so that other people will benefit from this, if they are trying to compare between two dates (comparing between day and month only, regardless of its year). Hope this is useful for others in the future.

Related

php - how to find the month names and numbers between two dates? [duplicate]

This question already has answers here:
Elegant way to get the count of months between two dates?
(10 answers)
Closed 6 years ago.
I need to find the month names and numbers between two dates
For the above two variables, how to find out the month numbers, ie) output will goes like this
aug2016,sep2016,oct2016,nov2016,dec2016,jan2017
<?php
$date1 = '2013-11-15';
$date2 = '2014-02-15';
$output = [];
$time = strtotime($date1);
$last = date('M-Y', strtotime($date2));
do {
$month = date('M-Y', $time);
$total = date('t', $time);
$output[] = $month;
$time = strtotime('+1 month', $time);
} while ($month != $last);
echo implode(",", $output);
?>
output
Nov-2013,Dec-2013,Jan-2014,Feb-2014
Try this one
<?php
$var1 = "2016-08-15";
$var2 = "2017-01-31";
$result = '';
while (date('m', strtotime($var1)) != date('m', strtotime($var2))) {
$result .= date('MY',(strtotime('next month',strtotime($var1)))).",";
$var1 = date('Y-m-d',(strtotime('next month',strtotime($var1))));
}
echo trim($result, ",");
?>
Output : Sep2016,Oct2016,Nov2016,Dec2016,Jan2017
Correct me if I'm wrong but, based on the output you provided, it seems what you really want to do is extract a month name from month number.
See this: http://www.php.net/manual/en/function.jdmonthname.php
$monthName = jdmonthname($month, 0);
I think you should be able to take it from there.

Am not able to check if month == equal datetime

I am trying to show results of each month.
Im having this for loop:
foreach ($overview as $day) {
$year = date("Y") - 1;
if ($day->user == $info->id) {
$startDate = new DateTime($day->Calendar_startdate);
$endDate = new DateTime($day->Calendar_enddate);
$s = $startDate->format('Y-m-d');
$e = $endDate->format('Y-m-d');
if ($s > $year) {
$workdays = number_of_working_days($s, $e);
$daysleft = $daysleft + $workdays;
} else {
}
}
}
This for loop is also in an if statement which echos the months.
Now I need to let it work for the months January, February etc...
I am able to not show results if in the previous year which works well.
If you want to compare $s with $year just change $year to :
$time = new DateTime('now');
/*** you can use `now` for today
/* or you can change to a fixed date exmp: 2016-01-01
*/
$year = $time->modify('-1 year')->format('Y-m-d');
Than you can compare $s > $year
I fixed by checking each month if it contained for example -01-
DB::table('Calendar')->where('Calendar_startdate', 'like','%' . $monthnumber . '%')->where('user', $info->id)->where('Calendar_type',2)->get();

Increment date when it should be a new month

I have a PHP script which records things based on the day. So it will have a weekly set of inputs you would enter.
I get the data correctly, but when i do $day ++; it will increment the day, going passed the end of the month without ticking the month.
example:
//12/29
//12/30
//12/31
//12/32
//12/33
Where it should look like
//12/29
//12/30
//12/31
//01/01
//01/02
My script is as follows:
$week = date ("Y-m-d", strtotime("last sunday"));
$day = $week;
$run = array(7); //this is actually defined in the data posted to the script, which is pretty much just getting the value of the array index for the query string.
foreach( $run as $key=>$value)
{
$num = $key + 1;
$items[] = "($num, $user, $value, 'run', '$day')";
echo "".$day;
$day ++;
}
Should I be manipulating the datetime differently for day incrementations?
You can use
$day = date("Y-m-d", strtotime($day . " +1 day"));
instead of
$day++;
See live demo in ideone
You refer to $day as a "datetime" but it is just a string - that is what date() returns. So when you do $day++ you are adding 1 to "2015-12-02". PHP will do everything it can to make "2015-12-02" into a number and then add 1 to it, which is not date math. Here is a simple example:
<?php
$name = "Fallenreaper1";
$name++;
echo $name
?>
This will output:
Fallenreaper2
This is how I would do it, using an appropriate data type (DateTime):
<?php
$day = new DateTime('last sunday');
$run = array(7);
foreach ($run as $key => $value) {
$num = $key + 1;
$dayStr = $day->format('Y-m-d');
$items[] = "($num, $user, $value, 'run', '$dayStr')";
echo $dayStr;
$day->modify('+1 day');
}
To increase time you should use strtotime("+1 day");
here is simple example of using it
<?php
$now_time = time();
for($i=1;$i<8;$i++) {
$now_time = strtotime("+1 day", $now_time);
echo date("Y-m-d", $now_time) . "<br>";
}
?>

Only show html on certain days w/php

I only want to show some html on certain days of the week. I think I am very close, but cannot get this to work. Thanks for the help.
<?php
$dayofweek = date('l');
$daystoshow = array('Thursday','Sunday','Wednesday');
if ($dayofweek == $daystoshow) {
echo "show this";
}
?>
Use in_array() to check to see if a value in in an array:
if (in_array($dayofweek, $daystoshow)) {
Allright:
Lets use PHP date function
Do you want to show it only during a particular month ?
$date = date("m");
if ($date == "01") { // only on january
//output
}
Do you want to show it only during a particular day ?
$date = date("d");
if ($date == "01") { //Only on the first of the month
//output
}
Do you want to show it only during particular dayS ?
$days = array("01","11","28");
$date = date("d");
if (in_array($date,$days)) { //Only 01,11,28
//output
}
Do you want to show it only during particular days of week ?
$days = array("Thursday","Sunday","Wednesday");;
$date = date("l");
if (in_array($date,$days)) {
//output
}
Do you want to show it only during a particular year ?
$date = date("Y");
if ($date == "2014") { //Only on 2014
//output
}

Compare Dates PHP

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... }

Categories