[英]PHP If given date is date range
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
(动态)是否是this week
, last week
, this month
, last month
和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?
我该如何解决?
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: 我修改了您的代码,如果需要进一步修改IF语句,但是日期创建按预期工作,您将获得DateTime对象,并且可以从它们中进行任何操作:
$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;
}
$dateJob
is never defined (third and fifth if statement). 从未定义
$dateJob
(if语句的第三和第五个)。
Maybe did you meant $scheduled_job
? 也许您是说
$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
格式,因为如果使用/
作为分隔符,则意味着m/d/y
$my_date = "29-02-2016";
Simply do str_replace
of '/'
slash: 只需对
str_replace
'/'
str_replace
做'/'
斜杠:
$my_date = str_replace('/', '.', '29/02/2016');
Because strtotime
documentation says: 因为
strtotime
文档说:
Dates in the m/d/y or dmy 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;
通过查看各个组成部分之间的分隔符,可以消除m / d / y或dmy格式的日期的歧义:如果分隔符为斜杠(/),则假定为美国m / d / y; whereas if the separator is a dash (-) or a dot (.), then the European dmy format is assumed.
相反,如果分隔符是破折号(-)或点(。),则采用欧洲dmy格式。
I pref using the DateTime class. 我喜欢使用DateTime类。
$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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.