在PHP中,获取今年第一天的日期可以通过多种方式实现。以下是一些常用的方法:
date()
和 strtotime()
$firstDayOfYear = date('Y-m-d', strtotime('first day of January this year'));
echo $firstDayOfYear; // 输出格式为:2023-01-01
DateTime
类$datetime = new DateTime('first day of January this year');
echo $datetime->format('Y-m-d'); // 输出格式为:2023-01-01
$year = date('Y');
$firstDayOfYear = $year . '-01-01';
echo $firstDayOfYear; // 输出格式为:2023-01-01
date()
函数:用于格式化本地时间/日期。strtotime()
函数:将任何英文文本的日期时间描述解析为 Unix 时间戳。DateTime
类:提供了面向对象的日期和时间处理方式。通过以上方法,你可以轻松地在PHP中获取今年的第一天,并应用于各种实际场景中。
领取专属 10元无门槛券
手把手带您无忧上云