Jump to content

Find Wed and Sat only after 11pm


snowman2344

Recommended Posts

Hello I am looking for help with the following function. What it does is get all the Wednesdays and Saturdays between 2 given dates. For that it works perfectly. I then use those dates with some more php to scrape a lottery site for the winning numbers and winning cash amounts. I am running into a problem when I use the function on a draw date. The lottery posts results after 11pm and if it is run before that time on the day of it returns the correct dates including the day of but the rest of my php errors because that lottery site is not updated. SO WHAT I AM LOOKING FOR is to run the function on the day of ONLY after 11pm

 

EG

If the start date = Wednesday July 11th

And  today = Wednesday July 25th

And I run the function at 2pm in the afternoon I want the script to return the following

 

Array ( [0] => 11 Jul 2012 [1] => 14 Jul 2012 [2] => 18 Jul 2012 [3] => 21 Jul 2012 )

 

If I run it after 11pm I want the function to return the following

 

Array ( [0] => 11 Jul 2012 [1] => 14 Jul 2012 [2] => 18 Jul 2012 [3] => 21 Jul 2012 [4] => 25 Jul 2012 )

 

But any date before 11pm the day of return all Wednesdays and Saturdays

 

function getDays($start,$end)
{ 
    $t = new DateTime($start ."12:00"); 
$e = new DateTime($end ." 12:00"); 
if ($e == 'Today'){
}	
    $out = array(); 
    for (; $t<=$e; $t->modify("+1 day")) { 
        $day = $t->format("D"); 
        if (in_array($day, array('Wed','Sat'))) { 
            $out[] = $t->format('d M Y'); 
        } 
    } 
     return $out; 
} 

//to call function i use the following

$dates = getDays($start_date, 'Today'); 

 

Thanks in advance

Link to comment
Share on other sites

Will you ever get a $end in the future? If not, something like this will work.

 

<?php

$start = 'July 11th 2012';
$end = 'July 25th 2012';

var_dump(getDays($start, $end));

function getDays($start, $end) {
$t = new DateTime($start . "12:00");
$e = new DateTime($end . " 12:00");
$out = array();
// prevent multiple date calls for speed. get current date and time
list($ymd, $time) = explode(' ', date('Y-m-d G'));
// check if $e is today, or later, and it's before 11PM
if( $e->format('Y-m-d') == $ymd && $time < 23 ) {
	// Set the end date back 1 so script never sees it
	$e->modify('-1 day');
}
for (; $t <= $e; $t->modify("+1 day")) {

	$day = $t->format("D");
	if (in_array($day, array('Wed', 'Sat'))) {
		$out[] = $t->format('d M Y');
	}
}
return $out;
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.