Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP的人类可读日期_Php_Date - Fatal编程技术网

使用PHP的人类可读日期

使用PHP的人类可读日期,php,date,Php,Date,请帮助我创建一个函数来转换日期,如 2010-10-04 17:45:50 变成更漂亮的样子 10 October at 17:45 因此,如果不是真的需要,它可以隐藏年份,并像这样显示日期 today/yesterday at 17:45 这有点类似于Facebook显示过去日期的方式。尝试使用此类: 我定义了一些constans来翻译成西班牙语,但是您可以忽略它们,或者为其他函数使用验证语言 您可以关注以下函数:shortDateHuman()、longDateHuman()、shor

请帮助我创建一个函数来转换日期,如

2010-10-04 17:45:50
变成更漂亮的样子

10 October at 17:45
因此,如果不是真的需要,它可以隐藏年份,并像这样显示日期

today/yesterday at 17:45
这有点类似于Facebook显示过去日期的方式。

尝试使用此类: 我定义了一些constans来翻译成西班牙语,但是您可以忽略它们,或者为其他函数使用验证语言

您可以关注以下函数:shortDateHuman()、longDateHuman()、shortTimeHuman()、longtimeuman()

为了使用它,您可以这样做:
$datetest=newdate($language)然后使用

$datetest->longDateHuman()

此外,如果需要,您可以检查函数setGMTOffset

class Date {
    var $gmtoffset;
    var $year;
    var $month;
    var $day;
    var $hour;
    var $minute;
    var $second;
    var $idioma;
   function Date($lang) {
        //setlocale (LC_TIME,en_US);
        list($this->year, $this->month, $this->day) = split("-", gmdate("Y-m-d"));
        list($this->hour, $this->minute, $this->second) = split(":", gmdate("g:i:s"));
        $this->gmtoffset = 0;
        $this->idioma=$lang;
    }
    function getYear() {
        return $this->year;
    }
    function setYear($year) {
        $year = intval($year);
        if($year >= 0) {
            $this->year = $year;
        } else {
            //throw new Exception("Invalid year! Prior to 0 A.D.!");
        }
        return true;
    }
    function getMonth() {
        return $this->month;
    }
    function setMonth($month) {
        $month = intval($month);
        if(($month >= 1) && ($month <= 12)) {
            $this->month = $month;
        } else {
            //throw new Exception("Invalid month! Not between 1 and 12!");
        }
        return true;
    }
    function getDay() {
        return $this->day;
    }
    function setDay($day) {
        $day = intval($day);
        if(($day >= 1) && ($day <= 31)) {
            $this->day = $day;
        } else {
            //throw new Exception("Invalid day! Not between 1 and 31!");
        }
        return true;
    }
    function getHour() {
        return $this->hour;
    }
    function setHour($hour) {
        $hour = intval($hour);
        if(($hour >= 0) && ($hour <= 23)) {
            $this->hour = $hour;
        } else {
            //throw new Exception("Invalid hour! Not between 0 & 23!");
        }
        return true;
    }
    function getMinute() {
        return $this->minute;
    }
    function setMinute($minute) {
        $minute = intval($minute);
        if(($minute >= 0) && ($minute <= 59)) {
            $this->minute = $minute;
        } else {
            //throw new Exception("Invalid minute! Not between 0 and 59!");
        }
        return true;
    }
    function getSecond() {
        return $this->second;
    }
    function setSecond($second) {
        $second = intval($second);
        if(($second >= 0) && ($second <= 59)) {
            $this->second = $second;
        } else {
            //throw new Exception("Invalid second! Not between 0 and 59!");
        }
        return true;
    }
    function getGMTOffset() {
        return $this->gmtoffset;
    }
    function setGMTOffset($gmtoffset) {
        $gmtoffset = intval($gmtoffset);
        if(($gmtoffset >= -12) && ($gmtoffset <= 12)) {
            $this->gmtoffset = $gmtoffset;
        } else {
            //throw new Exception("Invalid GMT offset! Not between -12 and 12!");
        }
        return true;
    }
    function setDate($year, $month, $day) {
        $this->setYear($year);
        $this->setMonth($month);
        $this->setDay($day);
        return true;
    }
    function setTime($hour, $minute, $second, $gmtoffset = 0) {
        $this->setHour($hour);
        $this->setMinute($minute);
        $this->setSecond($second);
        $this->setGMTOffset($gmtoffset);
        return true;
    }
    function setDateTime($year, $month, $day, $hour, $minute, $second, $gmtoffset) {
        $this->setDate($year, $month, $day);
        $this->setTime($hour, $minute, $second, $gmtoffset);
        return true;
    }
    function timestamp() {
        # This function returns a user's correct timezone in the form
        # of a unix timestamp.
        return mktime($this->hour + $this->gmtoffset,
                      $this->minute,
                      $this->second,
                      $this->month,
                      $this->day,
                      $this->year);
    }
    function longDateTimeHuman() {
        # Returns a date string like "Saturday, November 5, 2005 3:25 PM".
        return date("l, j F, Y g:i A", $this->timestamp());
    }
     function diaSemana(){
        # Returns a day string like "Saturday".
          return date("l", $this->timestamp());
    }
    function mes(){
        # retorna un string del mes "November".
          return date("F", $this->timestamp());
    }
    function diaSemana_es($diaen){
        switch($diaen)
    {
            case "Saturday": return("Sábado");break;
            case "Sunday": return("Domingo");break;
        case "Monday": return("Lunes");break;
            case "Tuesday": return("Martes");break;
            case "Wednesday": return("Miércoles");break;
        case "Thursday": return("Jueves");break;
            case "Friday": return("Viernes");break;
    }
   }
    function mes_es($mesen){
        switch($mesen)
    {      case "January": return(_JAN);break;
            case "February": return(_FEB);break;
        case "March": return(_MAR);break;
            case "April": return(_APR);break;
            case "May": return(_MAY);break;
        case "June": return(_JUN);break;
        case "July": return(_JUL);break;
        case "August": return(_AUG);break;
        case "September": return(_SEP);break;
        case "October": return(_OCT);break;
        case "November": return(_NOV);break;
            case "December": return(_DEC);break;
    }
   }
   function longDateHuman() {
        # Returns a date string like "Saturday, November 5, 2005".
         if($this->idioma=="en")
            return date("l, j F , Y", $this->timestamp());
             else
         {
             return "".$this->diaSemana_es($this->diaSemana()).", ".$this->mes_es($this->mes())." ".$this->day.", ".$this->year;
         }
    }
   function shortDateHuman() {
        # Returns a date string like "November 5, 2005".
        return date("F j, Y", $this->timestamp());
    }
   function longTimeHuman() {
        # Returns a time like "3:32:56 PM". F j of Y, g:i a
        return date("g:i:s A", $this->timestamp());
       // return date("F j of Y", $this->timestamp());
    }
    function shortTimeHuman() {
        # Returns a time like "3:32 PM".
        return date("g:i A", $this->timestamp());
    }
    function militaryTime() {
        # Returns a time like "15:34:24".
        return date("h:i:s", $this->timestamp());
    }
    function SQLDate() {
        return date("Y-m-d", $this->timestamp());
    }
    function SQLDate_convertir($timestamp){
    //  echo date("Y-m-d", $timestamp);
        //  echo  $timestamp;
      //  $aux=getdate($timestamp);
       // $aux=mktime($timestamp);
     //  $aux=gmstrftime($timestamp);
       //echo 
      // print_r(getdate($timestamp));
  //    $aux=$timestamp;
         //$aux=date("Y-m-d", $timestamp);
         $aux=split(" ",$timestamp);
         //print_r($aux);
         return $aux[0];
    }
    function SQLTime() {
        return date("h:i:s", $this->timestamp());
    }
    function SQLDateTime() {
        return date("Y-m-d h:i:s", $this->timestamp());
    }
}
上课日期{
var$gmtoffset;
var$年;
var$月;
var$天;
var$小时;
var$minute;
var$秒;
var$idioma;
功能日期($lang){
//设置地点(LC_时间,en_US);
列表($this->year,$this->month,$this->day)=拆分(“-”,gmdate(“Y-m-d”);
列表($this->hour,$this->minute,$this->second)=拆分(“:”,gmdate(“g:i:s”);
$this->gmtoffset=0;
$this->idioma=$lang;
}
函数getYear(){
本->年返还$;
}
功能设置年(年){
$year=intval($year);
如果($year>=0){
$this->year=$year;
}否则{
//抛出新异常(“公元0年之前的年份无效!”;
}
返回true;
}
函数getMonth(){
返回$this->month;
}
函数设置月($month){
$month=intval($month);
如果($month>=1)和($month-month=$month;
}否则{
//抛出新异常(“无效月份!不在1和12之间!”);
}
返回true;
}
函数getDay(){
返回$this->day;
}
函数设置日($day){
$day=intval($day);
如果(($day>=1)和($day day=$day;
}否则{
//抛出新异常(“无效日期!不在1和31之间!”);
}
返回true;
}
函数getHour(){
返回$this->hour;
}
函数设置小时(小时){
$hour=intval($hour);
如果($hour>=0)和($hour-hour=$hour;
}否则{
//抛出新异常(“无效小时!不在0和23之间!”);
}
返回true;
}
函数getMinute(){
返回$this->minute;
}
函数setMinute($minute){
$minute=intval($minute);
如果($minute>=0)和($minute-minute=$minute;
}否则{
//抛出新异常(“分钟无效!不在0和59之间!”);
}
返回true;
}
函数getSecond(){
返回$this->second;
}
函数设置秒($s){
$second=intval($second);
如果($second>=0)和($second second=$second;
}否则{
//抛出新异常(“无效秒!不在0和59之间!”);
}
返回true;
}
函数getGMTOffset(){
返回$this->gmtoffset;
}
函数设置gmtoffset($gmtoffset){
$gmtoffset=intval($gmtoffset);
如果($gmtoffset>=-12)和($gmtoffset-gmtoffset=$gmtoffset;
}否则{
//抛出新异常(“无效的GMT偏移量!不在-12和12之间!”);
}
返回true;
}
函数设置日期($year、$month、$day){
$this->setYear($year);
$this->setMonth($month);
$this->setDay($day);
返回true;
}
函数设置时间($hour、$minute、$second、$gmtoffset=0){
$this->setHour($hour);
$this->setMinute($minute);
$this->setSecond($second);
$this->setGMTOffset($gmtoffset);
返回true;
}
函数setDateTime($year、$month、$day、$hour、$minute、$second、$gmtoffset){
$this->setDate($year,$month,$day);
$this->setTime($hour、$minute、$second、$gmtoffset);
返回true;
}
函数时间戳(){
#此函数以以下形式返回用户的正确时区
#一个unix时间戳。
返回mktime($this->hour+$this->gmtoffset,
$this->minute,
$this->second,
$this->month,
$this->day,
$this->year);
}
函数longDateTimeHuman(){
#返回日期字符串,如“2005年11月5日星期六下午3:25”。
返回日期(“l,j F,Y g:i A”,$this->timestamp());
}
函数diaSemana(){
#返回类似“Saturday”的日期字符串。
返回日期(“l”,$this->timestamp());
}
函数mes(){
#“11月”的一天。
返回日期(“F”,$this->timestamp());
}
函数diaSemana_es($diaen){
交换机($diaen)
{
“星期六”案件:返回(“萨巴多”);休息;
案件“星期日”:返回(“多明戈”);休息;
案件“星期一”:返回(“Lunes”);休息;
案件“星期二”:返回(“马蒂斯”);休息;
案件“星期三”:返回(“米尔科勒”);休息;
“星期四”案件:返回(“朱庇斯”);休息;
案件“星期五”:返回(“维也纳”);休息;
}
}
功能模块($mesen){
交换机($mesen)
{案件“一月”:返回(一月);中断;
案例“二月”:返回(二月);中断;
案例“三月”:返回(三月);中断;
案例“四月”:返回(_APR);中断;
案例“可能”:返回(可能);中断;
案例“六月”:返回(六月);中断;
案例“七月”:返回(七月);中断;
案例“八月”:返回(八月);中断;
案例“九月”:返回(九月);中断;
案例“十月”:返回(十月);中断;
案例“11月”:返回(11月);中断;
案例“十二月”:返回(十二月);中断;
}
}
函数longDateHuman(){
#返回日期字符串,如“2005年11月5日星期六”。
如果($this->习语
public function FormatTime($timestamp)
{
    // Get time difference and setup arrays
    $difference = time() - $timestamp;
    $periods = array("second", "minute", "hour", "day", "week", "month", "years");
    $lengths = array("60","60","24","7","4.35","12");

    // Past or present
    if ($difference >= 0) 
    {
        $ending = "ago";
    }
    else
    {
        $difference = -$difference;
        $ending = "to go";
    }

    // Figure out difference by looping while less than array length
    // and difference is larger than lengths.
    $arr_len = count($lengths);
    for($j = 0; $j < $arr_len && $difference >= $lengths[$j]; $j++)
    {
        $difference /= $lengths[$j];
    }

    // Round up     
    $difference = round($difference);

    // Make plural if needed
    if($difference != 1) 
    {
        $periods[$j].= "s";
    }

    // Default format
    $text = "$difference $periods[$j] $ending";

    // over 24 hours
    if($j > 2)
    {
        // future date over a day formate with year
        if($ending == "to go")
        {
            if($j == 3 && $difference == 1)
            {
                $text = "Tomorrow at ". date("g:i a", $timestamp);
            }
            else
            {
                $text = date("F j, Y \a\\t g:i a", $timestamp);
            }
            return $text;
        }

        if($j == 3 && $difference == 1) // Yesterday
        {
            $text = "Yesterday at ". date("g:i a", $timestamp);
        }
        else if($j == 3) // Less than a week display -- Monday at 5:28pm
        {
            $text = date("l \a\\t g:i a", $timestamp);
        }
        else if($j < 6 && !($j == 5 && $difference == 12)) // Less than a year display -- June 25 at 5:23am
        {
            $text = date("F j \a\\t g:i a", $timestamp);
        }
        else // if over a year or the same month one year ago -- June 30, 2010 at 5:34pm
        {
            $text = date("F j, Y \a\\t g:i a", $timestamp);
        }
    }

    return $text;
}
echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'