Home php教程 php手册 获取指定日期为星期几

获取指定日期为星期几

Jun 13, 2016 am 10:45 AM
for Write Several and exist designation date Week experience Obtain process need project Project Development

 

之前在项目开发过程当中,需要写一个月历和一个周历,由于当时项目经验太少,保存时间的时候将格式化后的时间保存到数据库,并且还要判断当前一个月内所有天数里发生的事件,还用到了给出指定的日期判断是星期几的功能。所以,对后期时间处理造成了不必要的麻烦。

后来从网上查了一些相关资料,结合自己学习的知识,编写了一个简单的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” 博客

  

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo Mar 30, 2024 pm 07:26 PM

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 Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code Apr 09, 2024 pm 03:20 PM

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 A closer look at PyCharm: a quick way to delete projects Feb 26, 2024 pm 04:21 PM

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

Where to get Google security code Where to get Google security code Mar 30, 2024 am 11:11 AM

Where to get Google security code

How to remove the date that appears automatically when printing from PPT handouts How to remove the date that appears automatically when printing from PPT handouts Mar 26, 2024 pm 08:16 PM

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

PyCharm Practical Tips: Convert Project to Executable EXE File PyCharm Practical Tips: Convert Project to Executable EXE File Feb 23, 2024 am 09:33 AM

PyCharm Practical Tips: Convert Project to Executable EXE File

How to change the date into a pound sign in Excel How to change the date into a pound sign in Excel Mar 20, 2024 am 11:46 AM

How to change the date into a pound sign in Excel

PyCharm Tutorial: How to remove items in PyCharm? PyCharm Tutorial: How to remove items in PyCharm? Feb 24, 2024 pm 05:54 PM

PyCharm Tutorial: How to remove items in PyCharm?

See all articles