Skip to content

Instantly share code, notes, and snippets.

@ChezRD
Last active October 10, 2015 11:16
Show Gist options
  • Save ChezRD/4780b7b3a4978d00aad0 to your computer and use it in GitHub Desktop.
Save ChezRD/4780b7b3a4978d00aad0 to your computer and use it in GitHub Desktop.
Prepare delivery dates list without weekends
function prepareDatesList( $self_delivery = false, $force = false )
{
if ( !empty( $this->dates_list ) && is_array( $this->dates_list ) && $force === false )
{
return $this->dates_list;
}
setlocale( LC_ALL, 'ru_RU.UTF-8', 'rus_RUS.UTF-8', 'Russian_Russia.UTF-8' );
$this->dates_list = array();
$date = $temp = new DateTime( "now" );
$db_list = $this->select( 'SELECT * FROM `DATELIST` WHERE `ALLOWED` = "no"' );
$out_db_list = array();
foreach ( $db_list as $db_item )
{
$out_db_list[ intval( $db_item['YEAR'] ) ][ intval( $db_item['MONTH'] ) ][ intval( $db_item['DAY'] ) ] = 'true';
}
$current_hour = intval( $date->format('H') );
if ( $current_hour < 12 )
{
if ( $self_delivery )
{
if ( !isset( $out_db_list[ intval( $temp->format('Y') ) ][ intval( $temp->format('m') ) ][ intval( $temp->format('d') ) ] ) )
{
while( $temp->format( 'N' ) == 7 )
{
$temp->modify( '+1 day' );
}
if ( $temp->format( 'N' ) != 7 )
{
$this->dates_list[] = array(
'value' => $temp->format('U'),
'title' => strftime('%d %B, %A', $temp->format('U') ),
'day' => $temp->format('d'),
'month' => $temp->format('m'),
'year' => $temp->format('Y'),
);
}
}
$iterations = 8;
}
else
{
$iterations = 7;
}
}
else
{
if ( $self_delivery )
{
$iterations = 7;
}
else
{
$temp->modify( '+1 day' );
$iterations = 7;
}
}
for ( $i = 0; $i < $iterations; $i++ )
{
$temp->modify( '+1 day' );
if ( $temp->format( 'N' ) == 7 )
{
$temp->modify( '+1 day' );
}
// if ( isset( $out_db_list[ intval( $temp->format('m') ) ][ intval( $temp->format('d') ) ] ) )
// {
// $temp->modify( '+1 day' );
// }
while( isset( $out_db_list[ intval( $temp->format('Y') ) ][ intval( $temp->format('m') ) ][ intval( $temp->format('d') ) ] ) )
{
$temp->modify( '+1 day' );
if ( $temp->format( 'N' ) == 7 )
{
$temp->modify( '+1 day' );
}
}
//if ( !isset( $out_db_list[ $temp->format('m') ][ $temp->format('d') ] ) )
{
$this->dates_list[] = array(
'value' => $temp->format('U'),
'title' => strftime('%d %B, %A', $temp->format('U') ),
'day' => $temp->format('d'),
'month' => $temp->format('m'),
'year' => $temp->format('Y'),
);
}
}
return $this->dates_list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment