PHP显示前天,昨天,今天,几天前

<?php
function tranTime($time) {
    $rtime = date("m-d H:i",$time);
    $rtime2 = date("Y-m-d H:i",$time);
    $htime = date("H:i",$time);
    $time = time() - $time;
    if ($time < 60) {
        $str = '刚刚';
    } elseif ($time < 60 * 60) {
        $min = floor($time/60);
        $str = $min.' 分钟前';
    } elseif ($time < 60 * 60 * 24) {
        $h = floor($time/(60*60));
        $str = $h.'小时前 '.$htime;
    } elseif ($time < 60 * 60 * 24 * 3) {
        $d = floor($time/(60*60*24));
        if($d==1)
           $str = '昨天 '.$htime;
        else
           $str = '前天 '.$htime;
    } elseif ($time < 60 * 60 * 24 * 7) {
        $d = floor($time/(60*60*24));
           $str = $d.' 天前 '.$htime;
    } elseif ($time < 60 * 60 * 24 * 30) {
        $str = $rtime;
    } else {
        $str = $rtime2;
    }
    return $str;
}
$w_time = time() - 3600;
$times = tranTime($w_time);
echo $times;
?>

 

php秒数转换成多少天/多少小时/多少分

function get_stay_time($timestamp, $is_hour = 1, $is_minutes = 1)
{
    if(empty($timestamp) || $timestamp <= 60) {
        return false;
    }

    $time = time();
    $remain_time = $time - $timestamp;

    $day = floor($remain_time / (3600*24));
    $day = $day > 0 ? $day.'天' : '';
    $hour = floor(($remain_time % (3600*24)) / 3600);
    $hour = $hour > 0 ? $hour.'小时' : '';
    if($is_hour && $is_minutes) {
        $minutes = floor((($remain_time % (3600*24)) % 3600) / 60);
        $minutes = $minutes > 0 ? $minutes.'分' : '';
        return $day.$hour.$minutes;
    }

    if($hour) {
        return $day.$hour;
    }
    return $day;
}

javascript 获取当前时间

var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间

 

PHP获取一年中的每星期的开始日期和结束日期

<?php
function get_week($year) { 
    $year_start = $year . "-01-01"; 
    $year_end = $year . "-12-31"; 
    $startday = strtotime($year_start); 
    if (intval(date('N', $startday)) != '1') { 
        $startday = strtotime("next monday", strtotime($year_start)); //获取年第一周的日期 
    } 
    $year_mondy = date("Y-m-d", $startday); //获取年第一周的日期 
 
    $endday = strtotime($year_end); 
    if (intval(date('W', $endday)) == '7') { 
        $endday = strtotime("last sunday", strtotime($year_end)); 
    } 
 
    $num = intval(date('W', $endday)); 
    for ($i = 1; $i <= $num; $i++) { 
        $j = $i -1; 
        $start_date = date("Y-m-d", strtotime("$year_mondy $j week ")); 
 
        $end_day = date("Y-m-d", strtotime("$start_date +6 day")); 
 
        $week_array[$i] = array ( 
            str_replace("-", 
            ".", 
            $start_date 
        ), str_replace("-", ".", $end_day)); 
    } 
    return $week_array; 
}
?>
函数get_week()通过传入参数$year年份,获取当年第一天和最后一天所在的周数,计算第一周的日期,通过循环获取每一周的第一天和最后一天的日期。最后返回是一个数组。
想得到指定周数的开始日期和结束日期,比如2011年第18周的开始日期和结束日期,代码如下:
<?php
$weeks = get_week(2011); 
echo '第18周开始日期:'.$weeks[18][0].''; 
echo '第18周结束日期:'.$weeks[18][1]; 
?>

最后输出结果:

第18周开始日期:2011.05.02 
第18周结束日期:2011.05.08

原文链接:http://www.helloweba.com/view-blog-125.html