2013-03-22 16 views
9

Używam następującego skryptu do wyciągnięcia informacji kalendarza z bazy danych mysql i wyświetlenia go na stronie. Próbuję ponownie sformatować datę z standardowym formacie daty MySQL, ale podczas pobierania go z bazy danych otrzymuję następujący błąd:Ostrzeżenie: date_format() oczekuje, że parametr 1 będzie 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 

bazy danych (jak widać daty są zapisane poprawnie): enter image description here

skrypt:

<?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

data ($ ślubna, "d-m-Y"); –

+1

@Pramod Twoje parametry są niewłaściwe ... – Clive

+7

Myślę, że ludzie wpadają w panikę, gdy widzą ostrzeżenie i nie czytają tego. Ostrzeżenie mówi, że funkcja oczekuje instancji 'DateTime', ale przekazałeś' string', więc twoja zmienna '$ weddingdate' zawiera ciąg daty, więc potrzebujesz tylko stworzyć obiekt' DateTime' (np. 'Date_format (new DateTime ($ weddingdate), $ desiredFormat) ') – Leri

Odpowiedz

16

Najlepszy sposób to użyć DateTime obiekt przekonwertować datę.

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

Uwaga: Będzie to wsparcie dla PHP 5> = tylko 5.3.0.

+0

Dzięki, to już jest! :-) –

+0

Nie wiem, co to było zamknięte jako "zbyt lokalne" - z pewnością rozwiązałem mój problem. – russellmania

9

trzeba przekazać obiekt DateTime do tego func. Zobacz Manual: php

string date_format (DateTime $object , string $format) 

Można spróbować użyć:

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

Albo można również użyć:

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

Dlaczego nie spróbujesz to tak:

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

Albo można też po prostu nie podoba:

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

Próbowałem już trzeciego rozwiązania sean662 i pracowałem z funkcją now() zapisaną w INSERT sql an to jest jej wartość w funkcji date_create(). Następnie zmienna jest przekazywana przez funkcję date_format() i możesz mieć kolejność dat, którą chcesz.

Powiązane problemy