February 25, 2013

get all weekdays in php

Question by Keydi

I am thingking if how to get all the date of weekdays between a given date

example: I have a date given 2013-01-01 and 2013-20-01

It must return all date of weekdays like 2013-01-01

how could this be done using php

Answer by Starx

Look into DatePeriod Class, this is available from PHP 5.3.

Here is an example from the site

$begin = new DateTime( '2012-08-01' );
$end = new DateTime( '2012-08-31' );
$end = $end->modify( '+1 day' );

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){

       // You can manipulate the date here
    echo $date->format("Ymd") . "<br>";

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!