All pastes #978652 Raw Edit

Someone

public text v1 · immutable
·published 2008-04-09 18:34 UTC
rendered paste body
<?
/**
* $Author: tleen $
* $Date: 2005/03/15 15:04:40 $
*
* date handling
*
**/
//date_default_timezone_set("America/New_York");


include_once "{$CODEFLUENT}boolean.php";
include_once "{$CODEFLUENT}config.php";
include_once "{$CODEFLUENT}debug.php";
 

function date_copyright($year){

    $current_year = date('Y');
    if($current_year != $year) return "{$year} - {$current_year}";
    else return $year; 

}

function date_date_format($date=0,$format=0,$tz_offset=0){
    global $CODEFLUENT_TIMESTAMP_FORMAT,$NO_DATE_FORMAT;

    if(!$format) $format = $CODEFLUENT_TIMESTAMP_FORMAT;
    if($date){
        $new_date = $date + $tz_offset;
        $returner =  date($format,$new_date);
    }else $returner = $NO_DATE_FORMAT;

    debug("date_date_format({$date},'{$format}',{$tz_offset}) [{$returner}]");
    return $returner;
}

function date_reformat($formatted_date,$format,$tz_offset=0){
    $time = strtotime($formatted_date);
    $returner =  date_date_format($time,$format,$tz_offset);

    debug("date_reformat('{$formatted_date}','{$format}',{$tz_offset}) ['{$formatted_date}' -> {$time} -> '{$returner}']");
    return $returner;
}

function date_format_database_date($date_string,$format=0,$tz_offset=0){
    $date = strtotime($date_string);    
    if($date < 1) $date = 0;
    return format_date($date,$format,$tz_offset,$tz_offset);
}

function date_now($format=0,$tz_offset=0){
    global $CODEFLUENT_TIMESTAMP_FORMAT;

    if(!$format) $format = $CODEFLUENT_TIMESTAMP_FORMAT;
    
    $now = (time() + $tz_offset);
    return date($format,$now);
}

function date_adjust($unixtime=0,$amount=0,$subtract=0){
    global $CODEFLUENT_TIMESTAMP_FORMAT;

    if(!$unixtime) $unixtime = time();
    $formatted = date($CODEFLUENT_TIMESTAMP_FORMAT,$unixtime);
    $formatted = "{$formatted} {$amount}";
    if($subtract) $formatted = "{$formatted} ago";
    $returner = strtotime($formatted);

    debug("date_adjust({$unixtime},{$amount},{$subtract}) formatted[{$formatted}] [{$returner}]");
    return $returner;
}

function date_add($unixtime=0,$amount=0){
    return date_adjust($unixtime,"+ {$amount}");
}

function date_subtract($unixtime,$amount){
    return date_adjust($unixtime,$amount,1);
}

// deprciate use date add
function date_add_month($timestamp,$add=0){
    global $DATE_DAYS_IN_MONTH;

    $dateinfo = getdate($timestamp);
    $year = $dateinfo['year'];
    $month = $dateinfo['mon'];
    $day = $dateinfo['mday'];
    $hours = $dateinfo['hours'];
    $minutes  = $dateinfo['minutes'];
    $seconds  = $dateinfo['seconds'];

    $month = ($month + $add);
    $year_adjust = ($month/12);
    $month = ($month%12);
    $month = abs($month);

    if(!$month){ 
        $month = 12;
        $year_adjust--;
    }

    if($year_adjust) $year = ($year + $year_adjust);
    

    $max_days = $DATE_DAYS_IN_MONTH[$month];
    if($day > $max_days) $day = $max_days;

    return mktime($hours,$minutes,$seconds,$month,$day,$year);
}

function date_get_day_start($unixtime){
    $date = getdate($unixtime);
    $month = $date['mon'];
    $year = $date['year'];
    $day = $date['mday'];
    return mktime(0,0,0,$month,$day,$year);
}

function date_get_day_end($unixtime){
    $date = getdate($unixtime);
    $month = $date['mon'];
    $year = $date['year'];
    $day = $date['mday'];
    return mktime(23,59,59,$month,$day,$year);
}

function date_get_week_start($unixtime){
    $day_of_week = date('w',$unixtime);
    $unixtime = date_subtract($unixtime,"{$day_of_week} days");
    return date_get_day_start($unixtime);
}

function date_get_week_end($unixtime){
    $day_of_week = date('w',$unixtime);
    $days_to_go = (6 - $day_of_week);
    $unixtime = date_add($unixtime,"{$days_to_go} days");
    return date_get_day_end($unixtime);
}

function date_last_day_of_month($month,$year){
    
    $time = mktime(0,0,0,$month,0,$year); 
    $date =  getdate($time);
    return $date['mday'];
}

function date_get_month_start($unixtime){
    $date = getdate($unixtime);
    $month = $date['mon'];
    $year = $date['year'];
    return mktime(0,0,0,$month,1,$year);
}


function date_get_month_end($unixtime){
    $date = getdate($unixtime);
    $month = $date['mon'];
    $year = $date['year'];
    $day = date_last_day_of_month($month,$year);
    return mktime(23,59,59,$month,$day,$year);
}

function date_is_holiday($unixtime){
    global $CODEFLUENT_DATE_HOLIDAYS;
    $month_day = date_date_format($unixtime,'n-j');
    return $CODEFLUENT_DATE_HOLIDAYS[$month_day];
}

function date_get_next_weekday($unixtime,$weekday=0){
    global $DATE_WEEKDAYS;

    $debug_unixtime = date_date_format($unixtime);
    debug("date_get_next_weekday({$unixtime},{$weekday}) [{$debug_unixtime}] looking for the next [{$DATE_WEEKDAYS[$weekday]}]");
    $dateunixtime = $unixtime;
    $date = getdate($dateunixtime);
    $current_weekday = $date['wday'];
    debug("testing {$DATE_WEEKDAYS[$weekday]} against {$DATE_WEEKDAYS[$current_weekday]}");
    for($i = 0; ($current_weekday != $weekday); $i++){
        $dateunixtime = date_add($unixtime,"{$i} day");
        $date = getdate($dateunixtime);
        $current_weekday = $date['wday'];
        debug("testing {$DATE_WEEKDAYS[$weekday]} against {$DATE_WEEKDAYS[$current_weekday]}");
    }
   
    debug("date_get_next_weekday({$unixtime},{$weekday})  [{$dateunixtime}]");
    return $dateunixtime;
}

function date_is_today($unixtime){
    $now = time();
    $min = date_get_day_start($now);
    $max = date_get_day_end($now);

    return boolean_between($min,$unixtime,$max);
}

?>