PHP next second friday relative to current date

I have a cron job and it runs every second Friday to submit to the mailing list

0   8   *   *   5   test $(($(date +\%V)\%2)) -eq 1

      

How can I find out the next post date relative to the current date in php so that I can tell the user when the next post will be.

eg

@returns next second Friday date from random_date
function next_dispatch_date(random_date){
} 
echo "Next Dispatch on" . next_dispatch_date(date());

      

+3


source to share


2 answers


The first solution that came to my mind was the following:

$dt = new DateTime();
$dt->modify('next friday'); // Next friday from now
$dt->modify('next friday'); // Next friday from next friday

echo $dt->format('Y-m-d');

      

or, as the function outlined in your post:



function next_dispatch_date($timestamp){
    $dt = new DateTime();
    $dt->setTimestamp($timestamp);
    $dt->modify('next friday');
    $dt->modify('next friday');

    return $dt->format('Y-m-d');
}

echo "Next dispatch on ".next_dispatch_date(time());

      

Please note that compared to your original post I used time()

and not date()

which is used to format timestamps.

+3


source


you can use strtotime()

with relative format (see PHP manual )

i.e.



@returns next second Friday date from random_date
function next_dispatch_date($random_date){
  date('Y-m-d', strtotime('Friday next week '.$random_date));
}

      

Assuming it $random_date

's in "Ymd" format ...

0


source







All Articles