我想检查当前年份是否大于日期字符串(D-M-Y)这里是我的代码
$OldDate = "09-30-2011";
$OldYear = strtok($OldDate, '-');
$NewYear = date("Y");
if ($OldYear < $NewYear) {
echo "Year is less than current year"
} else {
echo "Year is greater than current year";
}
答案 0 :(得分:4)
您可以使用strtotime()
:
$OldDate = "2011-09-30";
$oldDateUnix = strtotime($OldDate);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
<强>更新强>
因为您使用的是非常规日期戳,所以必须使用不同的方法,例如:
$OldDate = "09-30-2011";
list($month, $day, $year) = explode("-", $OldDate);
$oldDateUnix = strtotime($year . "-" . $month . "-" . $day);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
注意:如果您希望始终确保strtotime
正确理解您的日期,请使用 YYYY-MM-DD 强>
答案 1 :(得分:1)
您可以通过以下方式实现目标:
$input_date = date("09-30-2011");
$input_date_arr = explode("-", $input_date);
$currYear = date("Y");
$inputYear = $input_date_arr[2];
if ($currYear > $inputYear) {
echo "Current year is greater than given year!";
} else {
echo "Current year is not greater than given year!";
}
答案 2 :(得分:0)
使用日期函数获取年份
$OldDate = date("Y",strtotime("09-30-2011"));
$NewYear = date("Y",strtotime("now"));
if($OldYear<$NewYear)
{
echo "Year is less than current year"
}
else
{
echo "Year is greater than current year";
}
答案 3 :(得分:0)
$OldDate = "09-30-2011";
$OldYear = date('Y',strtotime($OldDate));
$NewYear = date("Y");
if($OldYear<$NewYear)
{
echo "Year is less than current year"
}
else
{
echo "Year is greater than current year";
}
答案 4 :(得分:0)
您可以将字符串转换为时间戳,并使用当前时间戳
进行检查if(time($OldDate) < time()){
// do stuff
} else {
// do other stuff
}
答案 5 :(得分:0)
这样做,
$dateString = '2021-02-24';
$yr = date("Y", strtotime($dateString));
$mon = date("m", strtotime($dateString));
$date = date("d", strtotime($dateString));