1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| <?php
echo "=== 基础日期时间操作 ===\n";
$timestamp = time(); echo "当前时间戳: $timestamp\n";
echo "格式化时间: " . date('Y-m-d H:i:s', $timestamp) . "\n"; echo "中文格式: " . date('Y年m月d日 H:i:s', $timestamp) . "\n";
$formats = [ 'Y-m-d' => '年-月-日', 'Y-m-d H:i:s' => '年-月-日 时:分:秒', 'Y/m/d' => '年/月/日', 'M d, Y' => '英文月 日, 年', 'l, F j, Y' => '星期, 月份 日, 年', 'c' => 'ISO 8601格式', 'r' => 'RFC 2822格式' ];
echo "\n常用日期格式:\n"; foreach ($formats as $format => $description) { echo "$description ($format): " . date($format) . "\n"; }
$dateString = '2023-08-05 15:30:45'; $parsedTimestamp = strtotime($dateString); echo "\n解析日期字符串 '$dateString': $parsedTimestamp\n"; echo "转换回格式: " . date('Y-m-d H:i:s', $parsedTimestamp) . "\n";
$relativeFormats = [ 'now', '+1 day', '-1 week', '+2 months', 'next Monday', 'last Friday', 'first day of this month', 'last day of next month' ];
echo "\n相对时间解析:\n"; foreach ($relativeFormats as $format) { $timestamp = strtotime($format); echo "$format: " . date('Y-m-d H:i:s', $timestamp) . "\n"; }
class DateTimeHelper { public static function formatTimeDiff($timestamp1, $timestamp2 = null) { $timestamp2 = $timestamp2 ?: time(); $diff = abs($timestamp2 - $timestamp1); $units = [ '年' => 365 * 24 * 3600, '月' => 30 * 24 * 3600, '周' => 7 * 24 * 3600, '天' => 24 * 3600, '小时' => 3600, '分钟' => 60, '秒' => 1 ]; foreach ($units as $unit => $seconds) { if ($diff >= $seconds) { $count = floor($diff / $seconds); return $count . $unit . '前'; } } return '刚刚'; } public static function isWorkday($timestamp = null) { $timestamp = $timestamp ?: time(); $dayOfWeek = date('N', $timestamp); return $dayOfWeek >= 1 && $dayOfWeek <= 5; } public static function getDaysInMonth($year, $month) { return date('t', mktime(0, 0, 0, $month, 1, $year)); } public static function getQuarter($timestamp = null) { $timestamp = $timestamp ?: time(); $month = date('n', $timestamp); return ceil($month / 3); } public static function getZodiacSign($month, $day) { $signs = [ ['摩羯座', 1, 20], ['水瓶座', 2, 19], ['双鱼座', 3, 21], ['白羊座', 4, 20], ['金牛座', 5, 21], ['双子座', 6, 22], ['巨蟹座', 7, 23], ['狮子座', 8, 23], ['处女座', 9, 23], ['天秤座', 10, 24], ['天蝎座', 11, 23], ['射手座', 12, 22] ]; foreach ($signs as $i => $sign) { if (($month == $sign[1] && $day >= $sign[2]) || ($month == ($sign[1] % 12) + 1 && $day < $signs[($i + 1) % 12][2])) { return $sign[0]; } } return '摩羯座'; } public static function calculateAge($birthDate, $currentDate = null) { $currentDate = $currentDate ?: date('Y-m-d'); $birth = new DateTime($birthDate); $current = new DateTime($currentDate); return $birth->diff($current)->y; } }
echo "\n=== 日期时间工具类示例 ===\n";
$pastTime = strtotime('-2 hours -30 minutes'); echo "时间差: " . DateTimeHelper::formatTimeDiff($pastTime) . "\n";
echo "今天是工作日: " . (DateTimeHelper::isWorkday() ? '是' : '否') . "\n";
echo "2023年2月天数: " . DateTimeHelper::getDaysInMonth(2023, 2) . "\n"; echo "2024年2月天数: " . DateTimeHelper::getDaysInMonth(2024, 2) . "\n";
echo "当前季度: 第" . DateTimeHelper::getQuarter() . "季度\n";
echo "8月5日星座: " . DateTimeHelper::getZodiacSign(8, 5) . "\n";
echo "1990-05-15出生年龄: " . DateTimeHelper::calculateAge('1990-05-15') . "岁\n"; ?>
|