How to get week days in php?

后端 未结 7 1452
小鲜肉
小鲜肉 2021-01-22 08:02

I want to get Monday, Tuesday, Wednesday... using php function.

I have only numeric values for those like 1,2,3..7 where

1 = Monday 2 = Tuesday ... 7 = Sunday.<

相关标签:
7条回答
  • 2021-01-22 08:36

    My personal favorite...

    $day_start = date( "d", strtotime( "next Sunday" ) ); // get next Sunday
    for ( $x = 0; $x < 7; $x++ )
        $week_days[] = date( "l", mktime( 0, 0, 0, date( "m" ), $day_start + $x, date( "y" ) ) ); // create weekdays array.
    
    0 讨论(0)
  • 2021-01-22 08:36

    If all you have is a numeric value and not a complete timestamp, by far the easiest way is this:

    $weekdays = array('Monday', 'Tuesday', 'Wednesday', ...);
    $weekday = 0;
    echo $weekdays[$weekday];
    
    0 讨论(0)
  • 2021-01-22 08:36
    switch ($day) {
        case 0 : echo "Monday"; break;//if $day = 0  it will print Monday
        case 1 : echo "Tuesday"; break;
        case 2 : echo "Wednesday"; break;
    }
    
    0 讨论(0)
  • 2021-01-22 08:44

    The following should give you the day of the week for today (ex. tuesday):

    <? echo date("l"); ?>

    or for a specific date you can use something like this:

    <? echo date("l",strtotime("10 September 2000")); ?>

    For more info: http://www.php.net/manual/en/function.date.php

    If you have just a number that you are trying to convert to a day of the week, you could use the following:

    function convertNumberToDay($number) {
        $days = array('Sunday','Monday', 'Tuesday', 'Wednesday','Thursday','Friday','Saturday');
        return $days[$number-1]; // we subtract 1 to make "1" match "Sunday", "2" match "Monday"
    }
    echo convertNumberToDay(3); // prints "Tuesday"
    0 讨论(0)
  • 2021-01-22 08:46
    <?php
    $day = 2;
    echo date('l', mktime(0,0,$day,0,1)); // Tuesday
    ?>
    

    So you can create a simple function like this:

    function getDayName($dayNo) {
        return date('l', mktime(0,0,$dayNo,0,1));
    }
    

    Demo: http://www.ideone.com/hrITc

    0 讨论(0)
  • 2021-01-22 08:46

    If you simply want to convert 0 to "Monday", 1 to "Tuesday", etc. use an array.

    $day_of_week = array
    (
        0 => 'Monday',
        1 => 'Tuesday',
        2 => 'Wednesday',
        .
        6 => 'Sunday'
    );
    
    $day = 2;
    
    echo $day_of_week[$day];
    
    0 讨论(0)
提交回复
热议问题