吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 915|回复: 7
收起左侧

[求助] php如何获取当前月的第一天时间戳和最后一天时间戳呢?

[复制链接]
ppgjx 发表于 2022-9-19 17:12
php如何获取当前月的第一天时间戳和最后一天时间戳呢? 我是做java的 php这块很不熟悉 找了很多资料也没看到怎么获取

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

finillusion 发表于 2022-9-19 17:22
你好
你可以试试这样
当前月的第一天时间戳
strtotime(date( “Y-m-01”))
当前月的最后一天时间戳
方式一
strtotime(date( “Y-m-t”))
方式二
strtotime(date('Y-m-d',strtotime(date('Y-m-1',strtotime('next month')).'-1 day')))
ninanboy 发表于 2022-9-19 17:28
时间戳从零点开始
当前月第一天 strtotime(date("Y-m-01")
最后一天 strtotime(date("Y-m-t"))
crazyhl 发表于 2022-9-19 17:40
wyh757787026 发表于 2022-9-19 17:58
/**
     * 计算当前月的开始日期,结束日期
     * @Param string $date 日期 2021-07
     * @param bool $timestamp  返回格式 是否为时间戳
     * @Return  array
     */
    public static function getMonthStartAndEnd($date,$timestamp)
    {
       $firstDay = date('Y-m-01', strtotime($date));
       $lastDay = date('Y-m-d', strtotime("$firstDay +1 month -1 day"));
       if($timestamp){
           return array(strtotime($firstDay), strtotime($lastDay));
       }else{
           return array($firstDay, $lastDay);
       }
    }
emptynullnill 发表于 2022-9-19 18:22
同4楼。用carbon库很方便
bbpp520 发表于 2022-9-19 18:39
希望对你有帮助
[PHP] 纯文本查看 复制代码
// 获取当前月的第一天
echo date("Y-m-d", strtotime("first day of")).'<br>';
// 获取当前月的最后一天
echo date("Y-m-d", strtotime("last day of")).'<br>';
ytw6176 发表于 2022-9-19 20:23
[PHP] 纯文本查看 复制代码
function getthemonth($date){ 
   $firstday = date('Y-m-01', strtotime($date)); 
   $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day")); 
   return array($firstday,$lastday); 
} 
$today = date("Y-m-d"); 
$day=getthemonth($today); 
echo "当月的第一天: ".$day[0]." 当月的最后一天: ".$day[1];
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-16 04:48

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表