2015-10-03 13 views
7

Ho questo timestamp a 13 cifre 1443852054000 che voglio convertire in data e ora ma non ci riesco. Ho provato questo codice:Come convertire un timestamp Unix a 13 cifre in data e ora?

echo date('Y-m-d h:i:s',$item->timestamp); 

non funziona per me e anche questo

$unix_time = date('Ymdhis', strtotime($datetime)); 

e questo:

$item = strtotime($txn_row['appoint_date']); 
<?php echo date("Y-m-d H:i:s", $time); ?> 

cosa devo usare?

+1

è in millisecondi. rimuovi gli ultimi tre zeri e il tuo codice funzionerà. –

risposta

10

Ovviamente questo è il timestamp in millisecondi, non in secondi. Dividerlo per 1000 e utilizzare la funzione date:

echo date('Y-m-d h:i:s', $item->timestamp/1000); 
// e.g 
echo date('Y-m-d h:i:s',1443852054000/1000); 
// shows 2015-10-03 02:00:54 
+1

sto ancora ottenendo questo risultato 1970-01-01 01:00:00 –

+0

'var_dump ($ item-> timestamp);' –

+0

è null con var dump, non funziona –

1

È possibile raggiungere questo obiettivo con DateTime::createFromFormat.

perché hai un timestamp con 13 digits, dovrete dividerlo per 1000, al fine di utilizzarlo con DateTime, vale a dire:

$ts = 1443852054000/1000; // we're basically removing the last 3 zeros 
$date = DateTime::createFromFormat("U", $ts)->format("Y-m-d h:i:s"); 
echo $date; 
//2015-10-03 06:00:54 

DEMO

http://sandbox.onlinephpfunctions.com/code/d0d01718e0fc02574b401e798aaa201137658acb


.210

Si consiglia di set the default timezone per evitare eventuali avvisi

date_default_timezone_set('Europe/Lisbon'); 

NOTA:

più su php Data e ora in php the right way

Problemi correlati