[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Get the weekday of a date

Posted on 2010-09-21
7
Medium Priority
?
293 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 800 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 400 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 400 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
Industry Leaders: 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 800 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

656 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