SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Dates in a week

  1. #1
    SitePoint Zealot alix's Avatar
    Join Date
    Jun 2006
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Dates in a week

    Hi.

    I have 2 dates, the beginning and end date of a week, in format yyyy-mm-dd.

    I need to register in a mysql table a row for each day of that week and I can't seem to figure out how to do it.

    Can anybody help me ?

    Thanks in advance!!!

  2. #2
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    $monday 
    '2009-08-03';
    $monday strtotime($monday); // convert to timestamp

    $week = array();
    for (
    $i 0$i 7$i++) {
        
    $timestamp mktime(000date('m'$monday), date('d'$monday) + $idate('Y'$monday));
        
    $key date('D'$timestamp);
        
    $value date('Y-m-d'$timestamp);
        
    $week[$key] = $value;
    }

    echo 
    '<pre>';
    var_dump($week);
    echo 
    '</pre>';

    // now you can loop through $week with foreach and build INSERT query
    // then execute it to insert all rows to the table at once

  3. #3
    SitePoint Zealot alix's Avatar
    Join Date
    Jun 2006
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    PHP Code:
    <?php

    $monday 
    '2009-08-03';
    $monday strtotime($monday); // convert to timestamp

    $week = array();
    for (
    $i 0$i 7$i++) {
        
    $timestamp mktime(000date('m'$monday), date('d'$monday) + $idate('Y'$monday));
        
    $key date('D'$timestamp);
        
    $value date('Y-m-d'$timestamp);
        
    $week[$key] = $value;
    }

    echo 
    '<pre>';
    var_dump($week);
    echo 
    '</pre>';

    // now you can loop through $week with foreach and build INSERT query
    // then execute it to insert all rows to the table at once
    That worked great!!! Thanks!!!

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also do
    Code:
    insert into mytable (thedate) values ('2009-05-09' + INTERVAL 3 DAY)


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •