convert string to date php

后端 未结 2 732

hello i wanted to build

I get $date and $date1 from form xxx. I wanted to make leave program.

now i wanted process $date with variable string with value xx

相关标签:
2条回答
  • 2020-12-21 14:46

    You don't need substr or mysql for this. First get your dates without substr:

    $tawal = date('Y-m-d', strtotime($date));
    $takhir = date('Y-m-d', strtotime($date1));
    

    Now you have the Y-m-d formatted strings. To find the diff, though you don't have to convert to Y-m-d since we don't need mysql. You can use this method to find the difference in seconds.

    $diff = abs(strtotime($date2) - strtotime($date));
    $years = floor($diff / (365*60*60*24));
    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
    
    0 讨论(0)
  • 2020-12-21 14:51

    Here is my helpers for date:

    function date_as($date, $format = 'Verbal, hour:minute')
    {
        parse_date($date);
    
        $format = str_replace(array('verbal', 'Verbal'), array($date['verbal'], $date['Verbal']), $format);
    
        return str_replace(array_keys($date), array_values($date), $format);
    }
    
    function parse_date(&$date)
    {
        $unix   = is_numeric($date) ? $date : strtotime($date);
    
        $date = array(
            'month'     => strtolower(date('F', $unix)),
            'dayweek'   => strtolower(date('l', $unix)),
            'date'      => date('d', $unix),
            'year'      => date('Y', $unix),
            'hour'      => date('H', $unix),
            'minute'    => date('i', $unix),
            'second'    => date('s', $unix),
            'today'     => (date('Y-m-d',strtotime('now')) == date('Y-m-d', $unix)),
            'yesterday' => (date('Y-m-d', strtotime('now - 1 day')) == date('Y-m-d', $unix)),
            'tomorrow'  => (date('Y-m-d', strtotime('now + 1 day')) == date('Y-m-d', $unix)),
            'mint'      => date('m', $unix),
        );
    
        if ($date['yesterday'])
        {
            $date['verbal'] = 'yesterday';
        }
        elseif ($date['today'])
        {
            $date['verbal'] = 'today';
        }
        elseif ($date['tomorrow'])
        {
            $date['verbal'] = 'tomorrow';
        }
        else
        {
            $date['verbal'] = 'date month';
        }
    
        foreach (array('dayweek', 'month', 'verbal', 'date') as $p)
        {
            $date[ucfirst($p)] = mb_convert_case($date[$p], \MB_CASE_TITLE, 'UTF-8');
        }
    }
    

    Format, as you wish...

    0 讨论(0)
提交回复
热议问题