Solved

Get the weekday of a date

Posted on 2010-09-21
7
289 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 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 109

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

766 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