Reputation: 3224
I'm trying to determine if the given date $my_date
(dynamic) is this week
,last week
, this month
, last month
and last 3 months
$my_date = "29/02/2016";
$scheduled_job = strtotime($my_date);
$this_week = strtotime("first day this week");
$last_week = strtotime("last week monday");
$this_month = strtotime("first day this month");
$last_month = strtotime("first day last month");
$last_three_month = strtotime("first day -3 month");
if($scheduled_job > $this_week) {
echo 1;
}
if($scheduled_job < $this_week and $scheduled_job >= $last_week) {
echo 2;
}
if(strtotime($date_job) > $this_month) {
echo 3;
}
if($scheduled_job < $this_month and $scheduled_job >= $last_month) {
echo 4;
}
if(strtotime($date_job) > $last_three_month) {
echo 5;
}
Nothing is being displayed. How do i solve?
Upvotes: 1
Views: 111
Reputation: 1612
I modified your code, if needed further modify your IF statements, but date creation works as expected, you get DateTime objects and you can do whatever you like from them:
$my_date = "29/02/2016";
//this week,last week, this month, last month and last 3 months
$scheduled_job = DateTime::createFromFormat('d/m/Y', $my_date);
//test your date
//echo $scheduled_job->format('Y-m-d');
$this_week = new DateTime(date('Y-m-d',strtotime("first day this week")));
$last_week = new DateTime(date('Y-m-d',strtotime("last week monday")));
$this_month = new DateTime(date('Y-m-d',strtotime("first day this month")));
$last_month = new DateTime(date('Y-m-d',strtotime("first day last month")));
$last_three_month = new DateTime(date('Y-m-d',strtotime("first day -3 month")));
if($scheduled_job > $this_week) {
echo 1;
}
if($scheduled_job < $this_week and $scheduled_job >= $last_week) {
echo 2;
}
if($scheduled_job > $this_month) {
echo 3;
}
if($scheduled_job < $this_month and $scheduled_job >= $last_month) {
echo 4;
}
if($scheduled_job > $last_three_month) {
echo 5;
}
Upvotes: 2
Reputation: 1830
I pref using the DateTime class.
$date = new \DateTime();
$thisMonday = $date->modify('first day of this week'); // to get the current week's first date
$lastMonday = $date->modify('last monday'); // to get last monday
$firstDayThisMonth = $date->modify('first day of this month'); // to get first day of this month
$firstDayLastMonth = $date->modify('first day of this month'); // to get first day of last month
$firstDayThreeMonthAgo = new \DateTime($firstDayThisMonth->format('Y-m-d') . ' - 3 months'); // first day 3 months ago
$my_date = str_replace('/', '.', "29/02/2016");
$scheduled_job = new \DateTime($my_date);
// Now you can do the checks.
Upvotes: 1
Reputation: 5316
Simply do str_replace
of '/'
slash:
$my_date = str_replace('/', '.', '29/02/2016');
Because strtotime
documentation says:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
Upvotes: 1
Reputation: 29912
$dateJob
is never defined (third and fifth if statement).
Maybe did you meant $scheduled_job
?
Moreover, try to format $my_date
in a different manner because if you use /
as separator, this means m/d/y
$my_date = "29-02-2016";
Upvotes: 1