PHP code example of weinirumo / easydate

1. Go to this page and download the library: Download weinirumo/easydate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

weinirumo / easydate example snippets


// 获取从2016-6-8到现在过去了多少天
$since = '2016-6-8';
$pastDays = EasyDate::create()->since($since)->getPastDays();
echo '从'.$since.'到现在已经过去了'.$pastDays.'天'; // 从2016-6-8到现在已经过去了733天

// 获取从2016-6-8到2016-8-8过去了多少天
$since = '2016-6-8';
$to = '2016-8-8';
$pastDays = EasyDate::create()->since($since)->to($to)->getPastDays();
echo '从' . $since . '到' . $to . '过去了' . $pastDays . '天'; // 从2016-6-8到2016-8-8过去了61天

注意:since()方法会自动重置EasyDate的to属性,而from()方法不会
例如:
EasyDate::create()->since('2016-6-8')->to('2016-8-8')->getPastDays(); // 这里设置了to属性
EasyDate::create()->from('2016-6-8')->getPastDays(); // 61  from方法没有重置to属性
EasyDate::create()->since('2016-6-8')->getPastDays(); // 733 since方法会自动重置to属性


// 获取从2016-6-8到现在过去了多少个月
$since = '2016-8-8';
$pastMonths = EasyDate::create()->since($since)->getPastMonths();
echo '从'.$since.'到现在过去了'.$pastMonths.'个月'; // 从2016-8-8到现在过去了22个月


// 获取从2016-8-8到现在过去了几年
$since = '2016-8-8';
$pastYears = EasyDate::create()->since($since)->getPastYears();
$pastFullYears = EasyDate::create()->since($since)->getPastFullYears();
echo '从'.$since.'到现在大约过去了'.$pastYears.'年<br>';
echo '从'.$since.'到现在过去了'.$pastFullYears.'个整年';

// 从2016-8-8到现在大约过去了2年
// 从2016-8-8到现在过去了1整年