获取指定日期为星期几
Jun 13, 2016 am 10:45 AM
之前在项目开发过程当中,需要写一个月历和一个周历,由于当时项目经验太少,保存时间的时候将格式化后的时间保存到数据库,并且还要判断当前一个月内所有天数里发生的事件,还用到了给出指定的日期判断是星期几的功能。所以,对后期时间处理造成了不必要的麻烦。
后来从网上查了一些相关资料,结合自己学习的知识,编写了一个简单的function,现将代码贴出,仅供大家参考,有不足之处,忘多多批评。
1. /*
2. * 功能:获取指定年月日是星期几
3. * 传参:年月日格式:2010-01-01的字符串
4. * 返回值:计算出来的星期值
5. * 创建者:FrancisRan
6. * 创建时间:2010-07-13
7. * 最后修改时间:2010-07-13
8. * copyright (c)2010 ranfanwei1988@126.com
9. */
10. function transition ($date) {
11. $datearr = explode("-", $date); //将传来的时间使用“-”分割成数组
12. $year = $datearr[0]; //获取年份
13. $month = sprintf('%02d', $datearr[1]); //获取月份
14. $day = sprintf('%02d', $datearr[2]); //获取日期
15. $hour = $minute = $second = 0; //默认时分秒均为0
16. $dayofweek = mktime($hour, $minute, $second, $month, $day, $year); //将时间转换成时间戳
17. return date("w", $dayofweek); //获取星期值
18. }
本文出自 “FrancisRan” 博客

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo

Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code

A closer look at PyCharm: a quick way to delete projects

How to remove the date that appears automatically when printing from PPT handouts

PyCharm Practical Tips: Convert Project to Executable EXE File

How to change the date into a pound sign in Excel

PyCharm Tutorial: How to remove items in PyCharm?
