Get the Year/Month/Day from a datetime in php?

前端 未结 3 629
忘掉有多难
忘掉有多难 2021-01-31 06:57

I used date(\'w\', timestamp) and date(\'w\', timestamp) to know the day, date(\'n\', timestamp) for months, etc.

Now I\'m using d

相关标签:
3条回答
  • 2021-01-31 07:32

    Check out the manual: http://www.php.net/manual/en/datetime.format.php

    <?php
    $date = new DateTime('2000-01-01');
    echo $date->format('Y-m-d H:i:s');
    ?>
    

    Will output: 2000-01-01 00:00:00

    0 讨论(0)
  • 2021-01-31 07:42

    Try below code if you want to use php loop to display

    <span>
      <select name="birth_month">
        <?php for( $m=1; $m<=12; ++$m ) { 
          $month_label = date('F', mktime(0, 0, 0, $m, 1));
        ?>
          <option value="<?php echo $month_label; ?>"><?php echo $month_label; ?></option>
        <?php } ?>
      </select> 
    </span>
    <span>
      <select name="birth_day">
        <?php 
          $start_date = 1;
          $end_date   = 31;
          for( $j=$start_date; $j<=$end_date; $j++ ) {
            echo '<option value='.$j.'>'.$j.'</option>';
          }
        ?>
      </select>
    </span>
    <span>
      <select name="birth_year">
        <?php 
          $year = date('Y');
          $min = $year - 60;
          $max = $year;
          for( $i=$max; $i>=$min; $i-- ) {
            echo '<option value='.$i.'>'.$i.'</option>';
          }
        ?>
      </select>
    </span>
    
    0 讨论(0)
  • 2021-01-31 07:44

    Use DateTime with DateTime::format()

    $datetime = new DateTime($dateTimeString);
    echo $datetime->format('w');
    
    0 讨论(0)
提交回复
热议问题