Solved

Get the weekday of a date

Posted on 2010-09-21
7
285 Views
Last Modified: 2012-05-10
hi,

I have a start date and an end date; for example :

'2010-09-25 12:25:00' and '2011-06-30 23:59:59'

I would like to regroup all the mondays, tuesdays, wednesdays, etc. in an array :

$grid = array(
   1 => array(), // store Mondays between those two intervals (in format : 2010-09-25, time is not needed)
   2 => array(), // store Tuesdays between those two intervals
   3 => array(), // etc.
   4 => array(),
   5 => array(),
   6 => array(),
   0 => array()
);
0
Comment
Question by:matthew016
7 Comments
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 200 total points
Comment Utility
I think you could find very interesting this : http://www.phpclasses.org/package/3366-PHP-Iterate-the-over-the-days-of-a-range-of-dates.html. Registration is free.

Cheers
0
 
LVL 3

Assisted Solution

by:Progressed
Progressed earned 100 total points
Comment Utility
you could query the date like this:

SELECT *, DATE_FORMAT(your_column_name, 'N %d/%m/%Y') as date FROM tablename

See that I added 'N' in the date.

If you query $result['datetime']; you should get the name of the day automaticly... maybe you could array the result that contains monday or any other day now.

hope it helps
0
 
LVL 4

Assisted Solution

by:Parag_t567
Parag_t567 earned 100 total points
Comment Utility
Hi

i have attached the code. You will have to manipulate it a little to customise it according to your needs.

Thanks
Parag
<?php

function get_days ( $s, $e )
{
    $r = array ();
    $s = strtotime ( $s ); 
    $e = strtotime ( $e );

    $day = gmdate ( 'l', $s );

    do
    {
        $r[] = $day . ', ' . gmdate ( 'Y-m-d', $s );

        $s += 86400 * 7;

    } while ( $s <= $e );

    return $r;
}

print_r ( get_days ( '2007-02-05', '2007-02-20' ) );

?>

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Expert Comment

by:Progressed
Comment Utility
made a little mistake whit the N... offcourse thats numeric display instead of name day.

you must Use l (lower case L) to display day of week name.
0
 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 200 total points
Comment Utility
If you decide to download suggested class you can use it this way:

<?php

// test file for class dayIterator

require_once("day_iterator.php");

$from = strtotime("2010-09-25 12:25:00");
$to = strtotime("2011-06-30 23:59:59");

$dayIterator = new DayIterator($from, $to);

foreach($dayIterator as $day => $date)
{
    if (date("l", $date)=="Monday"){
        $mondays[] = $date;
    }
    // and so on
}
echo "<pre>";
var_dump($mondays);
echo "</pre>";
?>

IMHO it's a very good class.

Cheers
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
Comment Utility
Please read the article here.  Then install the code snippet and run it.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

HTH, ~Ray
<?php // RAY_temp_matthew016.php

error_reporting(E_ALL);

echo "<pre>";



// WHAT IS THE PHP VERSION?

echo PHP_EOL . 'PHP VERSION: ' . phpversion();



// SET UP SOME TEST DATA LIKE '2010-09-25 12:25:00' and '2011-06-30 23:59:59'

$alpha = strtotime('2010-09-25');

$omega = strtotime('2011-06-30');

$timestamps = range($alpha, $omega, 24*60*60);



// SHOW HOW PHP PRODUCES DATE FUNCTION RESULTS

foreach ($timestamps as $t)

{

    $text_date = date('r', $t);

    $grid[date('N', $t)][] = $text_date;

}



echo PHP_EOL . 'HERE ARE ALL THE MONDAYS:';

foreach ($grid[1] as $monday) { echo PHP_EOL . $monday; }



echo PHP_EOL . 'HERE IS THE GRID: ';

var_dump($grid);

Open in new window

0
 
LVL 9

Author Closing Comment

by:matthew016
Comment Utility
thx
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now