本文章来给大家介绍关于PHP计算上一个月的今天 今天是星期几的程序演示实例过程,各位有需要了解的朋友可参考。
上一个月的今天
strtotime 有个小问题
代码如下 | 复制代码 |
> php -r”echo date(‘Ymd000000′,strtotime ( ‘-1 month’, strtotime ( ’201307310000′ ) ));” |
此时,想起PHP中有一个mktime函数,于是写了如下代码
代码如下 | 复制代码 |
|
当执行时,发现结果和strtotime的结果是一样的。
搜了一下,下面的方法更准确一些
代码如下 | 复制代码 |
$time = strtotime("2011-03-31"); /** if ($last_month_t
return date("Y-m-t H:i:s", $last_month_time); return date(date("Y-m", $last_month_time) . "-d", $time); echo last_month_today($time); |
这里需要注意一点: date(”Y-m”, $last_month_time) . “-d” 这段代码。在写代码的过程中如果写成了 “Y-” . date(”m”, $last_month_time) . “-d” 则在跨年的时间上有问题
今天是星期几
代码如下 | 复制代码 |
|
恩…好像是对的..不过..这个函数有个问题,
如果月份的天数不同,结果就是错的.
加入今天是2012-03-31,计算上个月的今天,那么结果就是2012-03-03;
PHP有个函数 mktime,它可以取得日期的时间戳:
int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] )
根据给出的参数返回 Unix 时间戳。时间戳是一个长整数,包含了从 Unix 纪元(January 1 1970 00:00:00 GMT)到给定时间的秒数。
参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值。
所以可以通过它来计算
代码如下 | 复制代码 |
function last_month_day($time){ echo date('Y-m-d',$strtime); } last_month_day(strtotime("2012-03-31")); |
输出的结果是2012-03-02;按理说应该是输出2012-02-31????笨蛋2月有31号吗?没,有30号吗,没?有29号吗?..这个..可以有…
PHP给我们处理了这种情况,他会多出几天按下个月来算.
2012年2月最后一天是2012-02-29那么31号比29多两天,所以PHP就累加到下个月来处理 就是2012-03-02啦.
差点忘了..那么星期几怎么算呢??不用算啦..PHP给我们准备好了.
date(‘w’,$strtime);输出的就是一周中的第几天.就是星期几了.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
