Yan Susanto
Yan Susanto

Reputation: 353

convert string to date php

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-xx-xxxx(dd/mm/yyyy)and $date1 with value now i want to convert them to date.

I already know how to count day using datediff()

i convert $date so i can use dateadd() function

Here the code

$t1 = substr($date,0,2);
$b1 = substr($date,3,2);
$y1 = substr($date,6,4);

$t2 = substr($date11,0,2);
$b2 = substr($date1,3,2);
$y2 = substr($date1,6,4);

$tawal ="$y1-$b1-$t1";
$takhir = "$y2-$b2-$t2";

$query = "SELECT datediff('$takhir', '$tawal')as selisih";
$hasil = mysql_query($query);
$data = mysql_fetch_array($hasil);

$times = $data['selisih']; 
$times = + 1;

here the picture enter image description here

Upvotes: 2

Views: 1515

Answers (2)

devdRew
devdRew

Reputation: 4571

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...

Upvotes: 0

davidethell
davidethell

Reputation: 12018

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));

Upvotes: 6

Related Questions