2014-01-17 24 views
-3

我開始學習PHP OOP,並且我有這個練習來創建一個顯示日期的函數。 我想拿出這個結果: $ date = new(「14-01-2014」); //例如,這個用戶的日期以php,OOP方式創建顯示日期函數

echo $date -> displaDate ('YYYY-MM-JJ'); // Would result to 2014-01-14 
echo $date -> day; //Would result to 14 
echo $date -> dayOfWeek; //Would result to Thursday 

有人請向我解釋如何開始爲此做一個函數?我明白函數有變量和方法。

+1

爲什麼不看看[DateTime對象](http://www.php.net/manual/en/class.datetime.php)和[格式](http://www.php.net/ manual/en/datetime.format.php)方法 –

+0

看來你並不清楚函數,類,對象和方法之間有什麼區別。我建議你先學習一些理論。查找一些基本的OOP教程。 – s3v3n

+0

@ s3v3n我問這個問題是因爲我想學習理論,我也檢查了一些教程。 – swissed

回答

0

如果你到你自己實現它,你可以通過做這種方式去:

class Date 
{ 
    private $KeysArray; 

    // Members 
    public $day; 
    public $dayOfWeek; 

    public function __constructor() 
    { 
     // 1) This should get the current date and save it in the "keys array": 
     // the constants should of course be replaced with the method you're using to get 
     // the year, month or whatever. 
     $this->KeysArray = array("YYYY" => YEAR_VALUE, "MM" = MONTH_VALUE...) 
     // 2) Set all of the properties (data members): 
     $this->day = DAY_VALUE 
     $this->dayOfWeek = DAY_OF_WEEK_VALUE 
    } 

    public function DisplayDate($DateFormatString) 
    { 
     // 1) Parse the $DateFormatString. Turn it into an array of the terms. If it's  
     // always going to be split with "-", then you could use the function 
     // "explode()". 
     // 2) Go over that array and try calling each item with the $this->KeysArray. 
    } 
} 
+0

爲什麼不簡單地在DateTime中編寫一個包裝呢? –

+0

該課程旨在包裝PHP的日期,時間或DateTime功能之一。我認爲他的意思是他需要把自己作爲家庭作業來實現。 –

+0

你說得對,丹尼爾。謝謝! – swissed

1
<?php 

$dt = new DateTime(); 
echo $dt->format('j-n-Y'); 
?> 
+0

請檢查此類型。 –

0

很簡單....

<!DOCTYPE html> 
<html> 
<body> 

<?php 
$t=time(); 
echo($t . "<br>"); 
echo(date('Y-m-d',$t)); 
?> 

</body> 
</html> 
0

使用下面的代碼

<?php 

    class our_date 
    { 
     private $shortDateFormat = "F j, Y"; 
     private $longDateFormat = "F j, Y, g:i a"; //just mention you format here 
     private $dayofweek = "D"; //for example get the day of week 
     private $timestamp = 0; 

     function __construct($timestamp = 0) 
     { 
      $this->timestamp = $timestamp; 
     } 


     public function getTime() 
     { 
      return (int) $this->timestamp; 
     } 
     public function get_day_of_week() 
     { 
      return date($this->dayofweek,$this->timestamp); 
     } 
     public function long() 
     { 
      if ($this->timestamp > 0) 
      { 
       return date ($this->longDateFormat , $this->timestamp); 
      } 
      else 
      { 
       return ""; 
      } 
     } 

     public function short() 
     { 
      if ($this->timestamp > 0) 
      { 
       return date ($this->shortDateFormat , $this->timestamp); 
      } 
      else 
      { 
       return ""; 
      } 
     } 

     public function __toString() 
     { 
      return $this->timestamp; 
     } 

    } 

    /* Creating Date object*/ 

     $new_date_format = new our_date(time()); 

     /* echo current day of week */ 

     echo $new_date_format->get_day_of_week(); 
     echo '<br>'; 

     /* echo current date in short format */ 
     echo $new_date_format->short(); 
     echo '<br>'; 

     /* echo current date in long format */ 
     echo $new_date_format->long(); 
     echo '<br>'; 


    ?> 

努力去學習它,我已經實現了它對於本週日& 2個例子

從例子中學習並嘗試實施爲您的要求,其餘

無法實現做求人

0

@Daniel Saad,謝謝你幫助我理解。這是解決我的問題:

class Date{ 

    private $dateString;  

    function __construct($dateString) 
    { 
     $this->dateString = $dateString;  
    } 


    // Method to display date in different formats. 
    public function displayDate($dateFormat) 
    { 
     list($day, $month, $year) = explode('-', $this->dateString); 

     switch($dateFormat) 
     { 

      case "YYYY-mm-dd" : $format = $year.'-'.$month.'-'.$day; break; 
      case "mm-dd-YYYY" : $format = $month.'-'.$day.'-'.$year; break; 
      default : $format = $year.'-'.$month.'-'.$day; 
     } 
     return $format; 
    } 

    //Method to get day 
    public function day() 
    { 
     $day = date('d', strtotime($this->dateString)); 
     return $day; 
    } 

    //Method to get the day of the week. 
    public function dayofweek() 
    { 
     $weekday = date('l', strtotime($this->dateString)); 
     return $weekday; 

    } 
    //Method to get month in text. 
    public function getmonth() 
    { 
     $month = date('F', strtotime($this->dateString)); 
     return $month; 
    } 
} 


?> 

的index.php

<?php 
//Initialisation 
$date = new Date('14-01-2014'); 
$anotherdate = new Date('13-07-1979') 

?> 
    <h3>Instance 1</h3> 
    <p>Date: <?php echo $date->displayDate('YYYY-mm-dd'); ?></p> 
    <p>Day: <?php echo $date->day(); ?></p> 
    <p>Day of week: <?php echo $date->dayofweek(); ?></p> 
    <p>Month: <?php echo $date->getmonth(); ?></p> <br /> 

結果: 實例1 日期:2014年1月14日 日:本週14 日:星期二 月份:1月

我希望這也可以幫助那些誰是剛開始瞭解OOP。