当前位置:Gxlcms > PHP教程 > php取一个月的第一天的代码

php取一个月的第一天的代码

时间:2021-07-01 10:21:17 帮助过:15人阅读

本文介绍下,在php中根据时间戳取得一个月的第一天的实现代码,有兴趣的朋友,可以研究下哦。

在本节给出的这个函数,可以获取当月的第一天。 该函数接受一个单一的,可选的参数,它是一个UNIX时间戳的任何日期。 然后,该函数将返回从UNIX时间戳月份的第一天的UNIX时间戳。如果没有时间戳,该功能默认当前的月份。

由此产生的时间戳,可以结合php的日期函数date()进行任意可能的操作。 非常适合用作日历类,以及获取任何一个月的第一天。

代码:

  1. <!--?php
  2. /*
  3. *
  4. * @ 返回某月第一天的时间戳
  5. *
  6. * @param INT Unix timestamp
  7. *
  8. * @return INT
  9. *
  10. */
  11. function firstDayOfMonth($uts=null)
  12. {
  13. $today = is_null($uts) ? getDate() : getDate($uts);
  14. $first_day = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
  15. return $first_day[0];
  16. }
  17. /*** example usage ***/
  18. /*** using the default ***/
  19. echo firstDayOfMonth();
  20. echo '<hr /-->';
  21. /*** using a timestamp ***/
  22. $long_ago = strtotime('April 16 2002');
  23. echo firstDayOfMonth($long_ago);
  24. ?>

演示结果: 1375279200 1017583200

人气教程排行