I was wondering if it is possible to use PHP to figure out when a specific date it.
For example:
// Next week
$date->setDate(2014, 4, 3);
if($date->format('Y-m-d') == NEXT_WEEK){
// Do something
}elseif($date->format('Y-m-d') == NEXT_MONTH){
// DO something else
}elseif($date->format('Y-m-d') == NEXT_YEAR){
// Do something else
}
I understand my code won't work, but I am looking for something that would achieve something similar to that?
This can be achieved with DateTime objects using diff to see the time in between, something like this should do the trick:
// The date we want to check
$checkDate = new DateTime('2014-04-03', new DateTimeZone('UTC'));
// We need to compare it against today + a specific interval,
// so create a seperate DateTime object for today
$today = new DateTime('now', new DateTimeZone('UTC'));
// Check the difference between the dates
$diff = $today->diff($checkDate);
if ($diff->y == 0 && $diff->m == 0 && $diff->d == 7) {
// Date lies a week in the future
} elseif ($diff->y == 0 && $diff->m == 1 && $diff->d == 0) {
// Date lies a month in the future
} elseif ($diff->y == 1 && $diff->m == 0 && $diff->d == 0) {
// Date lies a year in the future
}
Easy Going with strtotime()
:
<?
$date = new DateTime();
$date->setDate(2014, 5, 2);
if($date->format('U') >= strtotime("next year")){
echo "next year";
}elseif($date->format('U') >= strtotime("next month")){
echo "next month";
}elseif($date->format('U') >= strtotime("next week")){
echo "next week";
}
?>
keep in mind, that just using >=
requires you to check FIRST for the year, then for month and then for week, cause strtotime("next year") > strtotime("next month") > strtotime("next week")
.
Based on the various definitions of "next week", "next month" and "next year", there are several ways to approach this that use the PHP date format parameter, explained in the PHP manual here. There is also a need to check the upper boundary to ensure that the date does not go past next week/month/year.
<?php
$checkdate = strtotime('+8 days');
$datenow = time();
$diff = $checkdate - $datenow;
echo "Today: ".date("Y-m-d", $datenow)."<br/>";
echo "Target: ".date("Y-m-d", $checkdate)."<br/>";
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$years = floor($diff / (365*60*60*24));
$daytoday = date ('N', $datenow);
$dayofmonthtoday = date ('j', $datenow);
$dayofmonthtarget = date ('j', $checkdate);
$monthtoday = date ('n', $datenow);
$monthtarget = date ('n', $checkdate);
$yeartoday = date ('Y', $datenow);
$yeartarget = date ('Y', $checkdate);
// $dayofyeartoday = date('z', $datenow);
// $dayofyeartarget = date('z', $checkdate);
if ($days >= 8-$daytoday && $days <= 14-$daytoday) {
echo "Next week (Monday onwards)<br/>";
}
if ($days >= 7-$daytoday && $days <= 13-$daytoday) {
echo "Next week (Sunday onwards)<br/>";
}
if ($days >= 7 && $days <= 13) {
echo "Next week (7-13 days)<br/>";
}
if ($monthtarget == $monthtoday + 1) {
echo "Next month (Change in month)<br/>";
}
if ($monthtarget == $monthtoday + 1 && $dayofmonthtarget >= $dayofmonthtoday) {
echo "Next month (Change in month but at least same day in the month and before end of that month)<br/>";
}
if ($yeartarget == $yeartoday + 1) {
echo "Next year (Change in year)<br/>";
}
if ($yeartarget == $yeartoday + 1 && $years == 1) {
echo "Next year (Change in year but at least same day and month in the year and before end of that year)<br/>";
}
?>