当前年份,特定日期和特定月份 - PHP

时间:2015-10-23 12:49:34

标签: php string date date-math

我想比较两个日期,但有些不对劲。

我的代码:

ERROR - 23.10.2015 09:46:32 --> Severity: Warning --> fwrite(): SSL operation     failed with code 1. OpenSSL Error messages:
error:140D00CF:SSL routines:SSL_write:protocol is shutdown         /home/[USER]/domains/[DOMAIN]/public_html/system/libraries/Email.php 2131

我的结果:

今天:2015/10/23 ChangeDay:2015-10-24 今天更大

但今天并不大。

2 个答案:

答案 0 :(得分:2)

您的日期格式不同,更不一样了。使用可比较的DateTime()个对象,或者将它们放在YYYY-MM-DD字符串格式中,这也是可比较的:

<?php

date_default_timezone_set('Europe/Lisbon');
$hoje = new DateTime();
$data_mudanca = new DateTime(date("Y")."-10-24");

if($hoje > $data_mudanca){
    echo 'Today is bigger';
}else{
    echo 'ChangeDay is bigger';
}

?>

<?php

date_default_timezone_set('Europe/Lisbon');
$hoje = date("Y-m-d");
$data_mudanca = date("Y")."-10-24";

if($hoje > $data_mudanca){
    echo 'Today is bigger';
}else{
    echo 'ChangeDay is bigger';
}

?>

答案 1 :(得分:2)

添加strtotime()php函数

    <?php
    date_default_timezone_set('Europe/Lisbon');
    $hoje = strtotime(date("Y/m/d"));
    $data_mudanca = strtotime(date("Y")."-10-24");

    echo 'Today: '.$hoje.'<br>';
    echo 'ChangeDay: '.$data_mudanca.'<br>';

    if($hoje > $data_mudanca){
        echo 'Today is bigger';
    }else{
        echo 'ChangeDay is bigger';
    }

    ?>