latest Post

Hitung tanggal antara tanggal awal dan tanggal akhir [PHP]

kasus ini aq temuin pas dsuruh itung total. dimana tu total rumusna ada jml hari yg diambil dari periode kontrak. mirip ultah ga? akhirnya ta cari d mbah google setelah nyerah pake perulangan [malah err TT ] c4p3 d33. hiks.. emang aq yg salah sih.
akhirnya ketemu fungsi nya pas nyari d google. 

Fungsi : ambil Tanggal antara startDate dan endDate

output : Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )


// Author : mybenchmarkid at yahoo dot com [output berbentuk array]

function DatesBetween($startDate, $endDate){
// get the number of days between the two given dates.
$days = (strtotime($endDate) - strtotime($startDate)) / 86400 + 1;
$startMonth = date(”m”, strtotime($startDate));
$startDay = date(”d”, strtotime($startDate));
$startYear = date(”Y”, strtotime($startDate));
$dates; //the array of dates to be passed back

for($i=0; $i<$days; $i++){
$pDates = explode(”/”, date(”n/j/Y”, mktime(0, 0, 0, $startMonth , ($startDay+$i), $startYear)));
$dates[$i] = $pDates[2].”/”.$pDates[0].”/”.$pDates[1];
}
return $dates;
}



2. source: http://boonedocks.net/mike/archives/137-Creating-a-Date-Range-Array-with-PHP.html

function createDateRangeArray($strDateFrom,$strDateTo)
{
    // takes two dates formatted as YYYY-MM-DD and creates an
    // inclusive array of the dates between the from and to dates.

    // could test validity of dates here but I'm already doing
    // that in the main script

    $aryRange=array();

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo>=$iDateFrom)
    {
        array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400; // add 24 hours
            array_push($aryRange,date('Y-m-d',$iDateFrom));
        }
    }
    return $aryRange;
}

$period = new DatePeriod(
     new DateTime('2010-10-01'),
     new DateInterval('P1D'),
     new DateTime('2010-10-05')
);
function date_range($first, $last, $step = '+1 day', $output_format = 'd/m/Y' ) {

    $dates = array();
    $current = strtotime($first);
    $last = strtotime($last);

    while( $current <= $last ) {

        $dates[] = date($output_format, $current);
        $current = strtotime($step, $current);
    }

    return $dates;
}

sumber : http://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array

About ummi hasya

ummi hasya
Recommended Posts × +

0 comments:

Post a Comment