Showing posts with label between date. Show all posts
Showing posts with label between date. Show all posts

Friday, 16 September 2016

How to calculate the difference between two dates using PHP




function DateDiff($startDate, $endDate) {
        
    $startDate = strtotime($startDate);
    $endDate = strtotime($endDate);

    if ($startDate === false || $startDate < 0 || $endDate === false || $endDate < 0 || $startDate > $endDate){
        return false;
    }

    $years = date('Y', $endDate) - date('Y', $startDate);

    $endMonth = date('m', $endDate);
    $startMonth = date('m', $startDate);

    /*** Calculate months ***/
    $months = $endMonth - $startMonth;
    if ($months <= 0) {
        $months += 12;
        $years--;
    }

    if ($years < 0){
        return false;
    }

    /*** Calculate the days  ***/
    $offsets = array();
    if ($years > 0){
        $offsets[] = $years . (($years == 1) ? ' year' : ' years');
    }
    if ($months > 0){
        $offsets[] = $months . (($months == 1) ? ' month' : ' months');
    }
    $offsets = count($offsets) > 0 ? '+' . implode(' ', $offsets) : 'now';

    $days = $endDate - strtotime($offsets, $startDate);
    $days = date('z', $days);

    return array($years, $months, $days);
}

Monday, 9 February 2015

Get post between dates in wordpress




$args = array(
'posts_per_page'   => -1,
'order'            => 'DESC',
'post_type'        => 'post',
'post_status'      => 'publish',
'cat'          => 59,
'date_query'  => array(
array(
'after'     => array(
'year'    => 2015,
'month'   => 02,
'day' => 06,
'hour' => 10,
'minute'  => 28,
'second'  => 55,
),
'before'    => array(
'year'    => 2015,
'month'   => 02,
'day' => 09,
'hour' => 15,
'minute'  => 24,
'second'  => 01,
),
),
),
);

$query_result = new WP_Query($args);