欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php+js+时间戳比较,输出不同内容

程序员文章站 2024-02-14 23:43:12
...

PHP函数 function switchInfor(infor1,infor2){ $mor_time = date(Y-m-d). 8:00:00; $nin_time = date(Y-m-d).17:00:00; $n_time = date(Y-m-d H:i:s); $mor_time_format = strtotime($mor_time); $nin_time_format = strtotime($nin_time); $n_time_format

PHP函数

function switchInfor(infor1,infor2){

$mor_time = date("Y-m-d")." 8:00:00";

$nin_time = date("Y-m-d")."17:00:00";

$n_time = date("Y-m-d H:i:s");

$mor_time_format = strtotime($mor_time);

$nin_time_format = strtotime($nin_time);

$n_time_format = strtotime($n_time);

if(($mor_time_format-$n_time_format)>=0&&($n_time_format-$nin_time_format)>=0)
return infor1;
else
return infor2;

}

JS函数

function switchInfor(infor1,infor2,idinfor){

//infor1 早上8点到晚上5之间的信息
//infor2 其他时间内的信息
//idinfor 输出位置的ID

var myDate = new Date(); //Date对象

var Year = myDate.getFullYear();

var Mouth = myDate.getMonth();

var Dat = myDate.getDate();

var mor_time = new Date(Year,Mouth,Dat,8,0,0).getTime();

var nin_time = new Date(Year,Mouth,Dat,17,0,0).getTime();

var now_time = myDate.getTime();

//alert(mor_time+'-'+nin_time+'-'+now_time);

if((now_time-mor_time>=0)&&(nin_time-now_time)>=0)
{
document.getElementById(idinfor).innerHTML=infor1;
}
else
{
document.getElementById(idinfor).innerHTML=infor2;
}
}