很多站长朋友们都不太清楚phpgetmonth,今天小编就来给大家整理phpgetmonth,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php当前时间夹30分钟 2、 PHP下获取上个月、下个月、本月的日期 3、 PHP如何获取本地时间与服务器时间 4、 PHP怎么实现动态时钟 php当前时间夹30分钟<?php
/*
* author: china_skag
* time: 2014-07-08
* 发博时间计算(年,月,日,时,分,秒)
* $createtime 可以是当前时间
* $gettime 你要传进来的时间
*/
class Mygettime{
function __construct($createtime,$gettime) {
$this->createtime = $createtime;
$this->gettime = $gettime;
}
function getSeconds()
{
return $this->createtime-$this->gettime;
}
function getMinutes()
{
return ($this->createtime-$this->gettime)/(60);
}
function getHours()
{
return ($this->createtime-$this->gettime)/(60*60);
}
function getDay()
{
return ($this->createtime-$this->gettime)/(60*60*24);
}
function getMonth()
{
return ($this->createtime-$this->gettime)/(60*60*24*30);
}
function getYear()
{
return ($this->createtime-$this->gettime)/(60*60*24*30*12);
}
function index()
{
if($this->getYear() > 1)
{
if($this->getYear() > 2)
{
return date("Y-m-d",$this->gettime);
exit();
}
return intval($this->getYear())." 年前";
exit();
}
if($this->getMonth() > 1)
{
return intval($this->getMonth())." 月前";
exit();
}
if($this->getDay() > 1)
{
return intval($this->getDay())." 天前";
exit();
}
if($this->getHours() > 1)
{
return intval($this->getHours())." 小时前";
exit();
}
if($this->getMinutes() > 1)
{
return intval($this->getMinutes())." 分钟前";
exit();
}
if($this->getSeconds() > 1)
{
return intval($this->getSeconds()-1)." 秒前";
exit();
}
}
}
//类的使用实例
/*
*
* 调用类输出方式
*
* $a = new Mygettime(time(),strtotime('-25 month'));
* echo iconv('utf-8', 'gb2312', $a->index())?iconv('utf-8', 'gb2312', $a->index()):iconv('utf-8', 'gb2312', '当前');
*
*/
PHP下获取上个月、下个月、本月的日期因为工作需要需要获取上个月、下个月、本月的日期,特从网站找到了实现代码,特分享下,方便需要的朋友
今天写程序的时候,突然发现了很早以前写的获取月份天数的函数,经典的switch版,但是获得上月天数的时候,我只是把月份-1了,估计当时太困了吧,再看到有种毛骨悚然的感觉,本来是想再处理一下的,但是一想肯定还有什么超方便的方法,于是找到了下面这个版本,做了一点小修改。
获取本月日期:
代码如下:
function
getMonth($date){
$firstday
=
date("Y-m-01",strtotime($date));
$lastday
=
date("Y-m-d",strtotime("$firstday
+1
month
-1
day"));
return
array($firstday,$lastday);
}
$firstday是月份的第一天,假如$date是2014-2这样的话,$firstday就会是2014-02-01,然后根据$firstday加一个月就是2014-03-01,再减一天就是2014-02-28,用date()和strtotime()真是太方便了。
获取上月日期:
代码如下:
function
getlastMonthDays($date){
$timestamp=strtotime($date);
$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
$lastday=date('Y-m-d',strtotime("$firstday
+1
month
-1
day"));
return
array($firstday,$lastday);
}
上月日期需要先获取一个时间戳,然后在月份上-1就OK了,超智能的date()会把2014-0-1这种东西转换成2013-12-01,太爽了。
获取下月日期:
代码如下:
function
getNextMonthDays($date){
$timestamp=strtotime($date);
$arr=getdate($timestamp);
if($arr['mon']
==
12){
$year=$arr['year']
+1;
$month=$arr['mon']
-11;
$firstday=$year.'-0'.$month.'-01';
$lastday=date('Y-m-d',strtotime("$firstday
+1
month
-1
day"));
}else{
$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));
$lastday=date('Y-m-d',strtotime("$firstday
+1
month
-1
day"));
}
return
array($firstday,$lastday);
}
下月日期的代码看起来比较长一点,因为date()转不了类似2014-13-01这种东西,它会直接回到1970,所以前面需要处理一下12月的问题,除了12月就直接月份+1就OK啦。
总得来说,还是很方便的,日期函数太强大了。
PHP如何获取本地时间与服务器时间PHP获取服务器时间和本地时间很简单,可以用内置函数实现,具体代码如下:
记得给分!!!
<?php
//获取服务器时间
echo date("Y-m-d h:i:s");
//获取本地时间(也可以说是你所在时区时间)
date_default_timezone_set('地区'); //地区:中国是PRC,或shanghai,chongqing都可
echo date("Y-m-d h:i:s");
?>
当然获取本地时间还可以用JS实现,如下:
<script type="text/javascript">
function showLocale(objD){
var str,colorhead,colorfoot;
var yy = objD.getYear();
if(yy<1900) yy = yy+1900;
var MM = objD.getMonth()+1;
if(MM<10) MM = '0' + MM;
var dd = objD.getDate();
if(dd<10) dd = '0' + dd;
var hh = objD.getHours();
if(hh<10) hh = '0' + hh;
var mm = objD.getMinutes();
if(mm<10) mm = '0' + mm;
var ss = objD.getSeconds();
if(ss<10) ss = '0' + ss;
var ww = objD.getDay();
if ( ww==0 ) colorhead="<font color=\"#000000\">";
if ( ww > 0 ww < 6 ) colorhead="<font color=\"#000000\">";
if ( ww==6 ) colorhead="<font color=\"#000000\">";
if (ww==0) ww="星期日";
if (ww==1) ww="星期一";
if (ww==2) ww="星期二";
if (ww==3) ww="星期三";
if (ww==4) ww="星期四";
if (ww==5) ww="星期五";
if (ww==6) ww="星期六";
colorfoot="</font>"
str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + ww + colorfoot;
return(str);
}
function tick(){
var today;
today = new Date();
document.getElementById("localtime").innerHTML = showLocale(today);
window.setTimeout("tick()", 1000);
}
tick();
</script>
PHP怎么实现动态时钟脚本说明:
第一步:把如下代码加入<body>区域中:
<script language="JavaScript">
function showMilitaryTime()
{
if (document.form.showMilitary[0].checked)
{
return true;
}
return false;
}
function showTheHours(theHour)
{
if (showMilitaryTime() || (theHour > 0 theHour < 13))
{
if (theHour == "0") theHour = 12;
return (theHour);
}
if (theHour == 0)
{
return (12);
}
return (theHour-12);
}
function showZeroFilled(inValue)
{
if (inValue > 9)
{
return "" + inValue;
}
return "0" + inValue;
}
function showAmPm()
{
if (showMilitaryTime())
{
return ("");
}
if (now.getHours() < 12)
{
return (" am");
}
return (" pm");
}
function showTheTime()
{
now = new Date
document.form.showTime.value = showTheHours(now.getHours()) + ":" + showZeroFilled(now.getMinutes()) + ":" + showZeroFilled(now.getSeconds()) + showAmPm()
setTimeout("showTheTime()",1000)
}
</script>
<FORM name=form><INPUT name=showTime size=11>
<P><INPUT CHECKED name=showMilitary type=radio>Military Time<BR><INPUT
name=showMilitary type=radio Unchecked> 12 Hour Time </FORM>
第二步:把<body>区域中的内容改为:
<body bgcolor="#fef4d9" onload=showTheTime()>
因为已经定义了函数了,也可以掉用
关于phpgetmonth的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于包含phpgetmonth的词条的详细内容...