Solved

Get the weekday of a date

Posted on 2010-09-21
7
287 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

785 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