2013-03-22 35 views
9

Tôi đang sử dụng tập lệnh sau để kéo thông tin lịch ra khỏi cơ sở dữ liệu mysql và hiển thị nó trên trang. Tôi cố gắng để tái định dạng ngày từ định dạng ngày chuẩn Mysql, nhưng khi lấy nó ra khỏi cơ sở dữ liệu là nhận được lỗi sau:Cảnh báo: date_format() hy vọng tham số 1 là DateTime

Warning: date_format() expects parameter 1 to be DateTime, string given in C:\easyphp\www\twinfocus\managecalendar.php on line 24 

Warning: date_format() expects parameter 1 to be DateTime, string given in C:\easyphp\www\twinfocus\managecalendar.php on line 24 

Warning: date_format() expects parameter 1 to be DateTime, string given in C:\easyphp\www\twinfocus\managecalendar.php on line 24 

Warning: date_format() expects parameter 1 to be DateTime, string given in C:\easyphp\www\twinfocus\managecalendar.php on line 24 

Cơ sở dữ liệu (như bạn sẽ nhìn thấy số ngày được lưu trữ một cách chính xác): enter image description here

kịch bản:

<?php 
    $sql2 = <<<SQL 
     SELECT * 
     FROM `calendar` 
    SQL; 
    if(!$result2 = $db->query($sql2)){ die('There was an error running the query [' . $db->error . ']');} 
    echo '<table class="admintable"> <thead>'; 
    echo '<tr><th>Client Names</th><th>Email</th><th>Tel</th><th>Wedding Date</th><th>Date Created</th><th>Start Time</th><th>End Time</th><th>Price</th><th>Location</th><th>Other Info</th><th>Edit</th><th>Delete</th></tr></thead>'; 
    while($row2 = $result2->fetch_assoc()){ 
    $weddingdate = $row2['weddingdate']; 
    $formattedweddingdate = date_format($weddingdate, 'd-m-Y'); 
    echo '<tr><td>'.$row2['name'].'</td><td>'.$row2['email'].'</td><td>'.$row2['tel'].'</td><td style="min-width:70px;">'.$formattedweddingdate.'</td><td style="min-width:70px;">'.$row2['datecreated'].'</td><td>'.$row2['starttime'].'</td><td>'.$row2['endtime'].'</td><td>&pound;'.$row2['price'].'</td><td>'.$row2['location'].'</td><td style="min-width:400px;">'.$row2['otherinfo'].'</td><td><a href="managecalendar.php?&key='.$key.'&editwedding='.$row2['id'].'">Edit</a></td><td><a href="calenderdelete.php?&key='.$key.'&delwedding='.$row2['id'].'">Delete</a></td></tr>';} 
    echo '</table>'; 

?> 
+1

ngày ($ weddingdate, 'd-m-Y'); –

+1

@Pramod Các thông số của bạn là vòng sai ... – Clive

+7

Tôi nghĩ mọi người hoảng sợ khi họ thấy cảnh báo và không đọc nó. Cảnh báo nói rằng hàm này trông đợi instance 'DateTime' nhưng bạn đã truyền' string' để biến '$ weddingdate' của bạn chứa chuỗi ngày tháng, vì vậy tất cả những gì bạn cần là tạo đối tượng' DateTime' (ví dụ 'date_format (new DateTime ($ weddingdate), $ mong muốnFormat) ') – Leri

Trả lời

16

Cách tốt nhất là sử dụng DateTime đối tượng để chuyển đổi ngày của bạn.

$myDateTime = DateTime::createFromFormat('Y-m-d', $weddingdate); 
$formattedweddingdate = $myDateTime->format('d-m-Y'); 

Lưu ý: Nó sẽ hỗ trợ cho PHP 5> = chỉ 5.3.0.

+0

Cảm ơn, thats đã nhận nó ngay bây giờ! :-) –

+0

Tôi không biết điều này đã bị đóng là "quá cục bộ" - chắc chắn đã giải quyết được vấn đề của tôi. – russellmania

9

Bạn cần phải vượt qua đối tượng DateTime để func này. Xem thủ công: php

string date_format (DateTime $object , string $format) 

Bạn có thể thử sử dụng:

date_format (new DateTime($time), 'd-m-Y'); 

Hoặc bạn cũng có thể sử dụng:

$date = date_create('2000-01-01'); 
echo date_format($date, 'Y-m-d H:i:s'); 
9

Tại sao bạn không thử nó như thế này:

$Weddingdate = new DateTime($row2['weddingdate']); 
$formattedweddingdate = date_format($Weddingdate, 'd-m-Y'); 

Hoặc bạn cũng có thể chỉ làm nó thích:

$Weddingdate = new DateTime($row2['weddingdate']); 
echo $Weddingdate->format('d-m-Y'); 
0

Tôi đã thử giải pháp thứ 3 của sean662 và đã làm việc với hàm now() được lưu trữ trong một giá trị INSERT sql sau đó là giá trị trong hàm date_create(). Sau đó biến này được chuyển qua hàm date_format() và bạn có thể có thứ tự ngày mà bạn thích.

Các vấn đề liên quan