strtotime函数的相关应用

    技术2022-05-20  74

    先来看几个应用:

    今日日期号

    CODE:$days=date('d',strtotime('now')); //2006-09-06 

    当前月月初

    CODE:echo $currMBegin=date("Y-m", strtotime('now'))."-01  "; 

    本月月末

    CODE:echo $currMEnd =date("Y-m-d", strtotime("next month -$days day"));

    下月月初

    CODE:echo $nextMBegin=date("Y-m", strtotime('next month'))."-01"; 

    下月月底

    CODE:echo $nextMEnd =date("Y-m-d", strtotime("+2 months -$days day")); 

    //2006-09-01 2006-09-30

    //2006-10-01 2006-10-31

     

     

     

    以下为转载:

    “打印上一个月第一天的时间”,面试题的第一道这么写着。

         记得还是之前在一个公司实习的时候,让我去做个留言本,其中有个要求是对时间格式的转换,那会使用了strtotime()函数,除此以外,一直都没怎么应用它。今天去面试之前特意看了下“打印前一天时间”的格式转化题目,是:

         echo date(”Y-m-d h:i:s”,strtotime(”-1 day”));

         然而,这回笔试还是没有想出具体应该怎么弄。回来后,同学告诉我应该是:

         echo date(”Y-m-01,h:i:s”,strtotime(”-1 moth”));

         想想这个strtotime()还是挺神奇的,如果能灵活的运用它,肯定会给工作带来不少方便,但PHP的手册中却对此函数的参数没作太多介绍,对些函数的其他介绍也非常少。整理相关资料如下:

         先看手册介绍:

        strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳

         格式:int strtotime ( string $time [, int $now ] )

         本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间。

         本函数将使用 TZ 环境变量(如果有的话)来计算时间戳。自 PHP 5.1.0 起有更容易的方法来定义时区用于所有的日期/时间函数。此过程在 date_default_timezone_get函数页面中有说明。

        Note: 如果给定的年份是两位数字的格式,则其值 0-69 表示 2000-2069,70-100 表示 1970-2000。参数time:被解析的字符串,格式根据 GNU » 日期输入格式的语法。在 PHP 5.0 之前,time 中不允许有毫秒数,自 PHP 5.0 起可以有但是会被忽略掉。now:用来计算返回值的时间戳;该参数默认值是当前时间time(),也可以设置为其他时间的时间戳。

        返回值:成功则返回间戳,否则返回 FALSE(在 PHP 5.1.0 之前本函数在失败时返回 -1,后面版本返回false)。

        strtotime的第一个参数可以是我们常见的英文时间格式,比如“2008-8-20”或“10 September 2000”等等。也可以是以参数now为基准的时间描述,比如“+1 day”等等。

        下面是后一种方式的可使用参数清单,其中“当前时间”是指strtotime第二个参数now的值,默认为当前时间。

        1. 月,日英文名及其常用缩写清单:january,february,march,april,may,june,july,august,september,sept,october,november,december,sunday,monday,tuesday,tues,wednesday,wednes,thursday,thur,thurs,friday,saturday

        2. 时间参数和祥细描述:am: the time is before noon 上午pm: the time is noon or later 下午year: one year; for example, “next year” 年,比如“next year”代表明年month: one month; for example, “last month” 月,比如“last month”代表上一月fortnight: two weeks; for example, “a fortnight ago” 两周,比如“a fortnight ago”代表两周前week: one week 周day: a day 天hour: an hour 小时minute: a minute 分钟min: same as minute 同”minute”second: a second 秒sec: same as second 同”second”

        3.相关和顺序说明:+n/-n:以当前时间算,加个减指定的时间,比如”+1 hour”是指当前时间加一小时ago:time relative to now; such as “24 hours ago”  以当前时间往前算,比如”24 hours ago”代表“24小时前”tomorrow: 24 hours later than the current date and time 以当前时间(包括日期和时间)为标准,明天同一时间yesterday: 24 hours earlier than the current date and time 以当前时间(包括日期和时间)为标准,昨天同一时间today: the current date and time 当前时间(包括日期和时间)now: the current date and time 当前时间(包括日期和时间)last: modifier meaning “the preceding”; for example, “last tuesday” 代表“上一个”,比如“last tuesday”代表“上周二同一时间”this: the given time during the current day or the next occurrence of the given time; for example, “this 7am” gives the timestamp for 07:00 on the current day, while “this week” gives the timestamp for one week from the current time 当天的指定时间或下面一个时间段的时间戳,比如“this 7am”给出当天7:00的时间戳,而“this week”给出的是从当前时间开始的一整周的时间戳,也就是当前时间(经本人测试:strtotime(’this week’)=strtotime(’now’));next: modifier meaning the current time value of the subject plus one; for example, “next hour” 当前时间加上指定的时间,比如“next hour”是指当前时间加上一小时,即加3600

    first: ordinal modifier, esp. for months; for example, “May first” (actually, it’s just the same as next)third: see first (note that there is no “second” for ordinality, since that would conflict with the second time value)fourth: see firstfifth: see firstsixth: see firstseventh: see firsteighth: see firstninth: see firsttenth: see firsteleventh: see firsttwelfth: see first

        4. 时区描述:gmt: Greenwich Mean Timeut: Coordinated Universal Timeutc: same as utwet: Western European Timebst: British Summer Timewat: West Africa Timeat: Azores Timeast: Atlantic Standard Timeadt: Atlantic Daylight Timeest: Eastern Standard Timeedt: Eastern Daylight Timecst: Central Standard Timecdt: Central Daylight Timemst: Mountain Standard Timemdt: Mountain Daylight Timepst: Pacific Standard Timepdt: Pacific Daylight Timeyst: Yukon Standard Timeydt: Yukon Daylight Timehst: Hawaii Standard Timehdt: Hawaii Daylight Timecat: Central Alaska Timeakst: Alaska Standard Timeakdt: Alaska Daylight Timeahst: Alaska-Hawaii Standard Timent: Nome Timeidlw: International Date Line Westcet: Central European Timemet: Middle European Timemewt: Middle European Winter Timemest: Middle European Summer Timemesz: Middle European Summer Timeswt: Swedish Winter Timesst: Swedish Summer Timefwt: French Winter Timefst: French Summer Timeeet: Eastern Europe Time, USSR Zone 1bt: Baghdad Time, USSR Zone 2zp4: USSR Zone 3zp5: USSR Zone 4zp6: USSR Zone 5wast: West Australian Standard Timewadt: West Australian Daylight Timecct: China Coast Time, USSR Zone 7jst: Japan Standard Time, USSR Zone 8east: Eastern Australian Standard Timeeadt: Eastern Australian Daylight Timegst: Guam Standard Time, USSR Zone 9nzt: New Zealand Timenzst: New Zealand Standard Timenzdt: New Zealand Daylight Timeidle: International Date Line East


    最新回复(0)