开发者

Apply a pattern against a calendar

Say I have a pattern of letters that simply r开发者_开发技巧epeats itself forever - is there an easy way to apply this pattern to a calendar?

For example, the actual pattern itself is E1, E, E, E, O, O, N1, N1, N, O, O, O, L, L1, L1, L, O, O, E, E, E1, O, O, N, N, N, N, O, O, O, L, L, L, O, O, E1, E, E, E, O, O

I would like to then take this pattern and apply it for the rest of the year (and if possible, a year or two after that aswell) starting from a given date, in my case, the 25th February 2012.

I've been thinking about how to solve this using PHP without having to resort to doing it manually but I can't think of the steps to take, all I can think is that it will make extensive use of the date() function!

All joking aside, I think putting these values into an array might be a good first step and use that structure as the basis of the loop?

Ideal output would simply be:

dd/mm/yyyy : one of E1/E/O/N1/N


Not sure why this was deleted, it was almost correct. All I added is the while loop from the previous answer. All credit to original poster.

<?php
$str = "E1, E, E, E, O, O, N1, N1, N, O, O, O, L, L1, L1, L, O, O, E, E, E1, O, O, N, N, N, N, O, O, O, L, L, L, O, O";
$now = strtotime('25th Feb 2012'); // Now in unix-timestamp (seconds since epoch)
$end = strtotime('2013-02-25');
while ($now <= $end)
{
    foreach (explode(', ', $str) as $val) { // Loop through each value
        print date("d/m/Y", $now) . " : {$val}<br />\n"; // Format output
        $now += 86400; // Add 1 day to $now (86400 seconds)
        if ($now >= $end) break; // Close after $end date
    }
}
?>


In php it's very easy using a Julian Daten

$str = explode(', ',"E1, E, E, E, O, O, N1, N1, N, O, O, O, L, L1, L1, L, O, O, E, E, E1, O, O, N, N, N, N, O, O, O, L, L, L, O, O");
$now = GregorianToJD(date('m'),date('j'),date('Y'));
$start = GregorianToJD(1,13,1993); // Start January 13th 1993
$days = $now-$start; //get total days
$tod = $str[($days % count($str)) - 1]; //Total days mod amount of options -1 gets you the key to the currect code.
echo "today is $tod";


You can use a function like this:

<?php
    function valueFor($check_date){
        //the data reference
        $data = "E1, E, E, E, O, O, N1, N1, N, O, O, O, L, L1, L1, L, O, O, E, E, E1, O, O, N, N, N, N, O, O, O, L, L, L, O, O";
        $data_ary = explode(',',str_replace(' ','',$data));
        $start_stamp = strtotime("2012-02-25");
        $check_stamp = strtotime($check_date);
        //get difference of days for both dates
        $days_diff = floor( ($check_stamp - $start_stamp) / (60*60*24) );
        if($days_diff > count($data_ary)) $days_diff = ( $days_diff % count($data_ary) ) - 1;
        return $data_ary[$days_diff];
    }

    //=> this will output 'E1'
    echo valueFor('2012-04-01'); 
?>

So you simply call the function passing the date.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜