How to get the week day of a date?

Discussion in 'PHP' started by amenda, Sep 26, 2008.

  1. #1
    I tried to use the following to get the week day of a date without success:

    No matter what the date in database is, it will all print out Wednesday. The date was recorded as date("Y/m/d"). And I tried the same script on different servers, still the same.

    What's wrong with the script?
     
    amenda, Sep 26, 2008 IP
  2. almondj

    almondj Peon

    Messages:
    768
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    <?php 
    
    $time = date(l);
    
    echo $time; 
    
    ?>
    PHP:
    The "l" in the parentheses is for the day of the week, Monday - Sunday ;).

    Full database insertion and stuff:

    
    <?php
    
    $time = date(l);
    
    echo "Today is: $time ";
    
    $db_host = "localhost";
    $db_user = "username";
    $db_pwd = "password";
    $db_name = "database";
    mysql_connect($db_host, $db_user, $db_pwd);
    mysql_select_db($db_name);
    
    if($time == "Thursday") {
    	mysql_query("INSERT INTO time (time) VALUES ('$time')");
    		{
    		echo "Thursday? Wow, that's a day of the week!";
    		}
    	}
    else {
    	mysql_query("INSERT INTO time (time) VALUES ('$time')");
    		{
    
                    echo "The date of $time was inserted into the database!";
    
    		}
    
    }
    
    ?>
    
    PHP:
     
    almondj, Sep 26, 2008 IP
  3. amenda

    amenda Banned

    Messages:
    778
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I already recorded all the date with the format of date("Y/m/d"), now I want to get the week day of the date.
     
    amenda, Sep 26, 2008 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    the problem is the getdate() read timestamps.
    convert the $sDate to timestamp first.
    $sDate = strtotime($list['date']); 
    // continue with your script
    PHP:
    it should work now.

    This is another way:
    $date = '2008/9/24'; // or your $list['date']
    $timestamp = strtotime($date); // make it a timestamp
    $weekday = date("l",$timestamp); // the weekday
    print $weekday;
    PHP:
     
    ads2help, Sep 26, 2008 IP