Solved

Get the weekday of a date

Posted on 2010-09-21
7
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 200 total points
ID: 33723892
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
ID: 33723902
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
ID: 33723903
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Progressed
ID: 33723958
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 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 200 total points
ID: 33724847
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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 33725095
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
ID: 33848474
thx
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 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