Esiste qualche funzione timetostr
in php che emetterà today/tomorrow/next sunday/etc.
da un determinato timestamp? In modo che timetostr(strtotime(x))=x
php strtotime reverse
risposta
Questo potrebbe essere utile per le persone che vengono qui.
/**
* Format a timestamp to display its age (5 days ago, in 3 days, etc.).
*
* @param int $timestamp
* @param int $now
* @return string
*/
function timetostr($timestamp, $now = null) {
$age = ($now ?: time()) - $timestamp;
$future = ($age < 0);
$age = abs($age);
$age = (int)($age/60); // minutes ago
if ($age == 0) return $future ? "momentarily" : "just now";
$scales = [
["minute", "minutes", 60],
["hour", "hours", 24],
["day", "days", 7],
["week", "weeks", 4.348214286], // average with leap year every 4 years
["month", "months", 12],
["year", "years", 10],
["decade", "decades", 10],
["century", "centuries", 1000],
["millenium", "millenia", PHP_INT_MAX]
];
foreach ($scales as list($singular, $plural, $factor)) {
if ($age == 0)
return $future
? "in less than 1 $singular"
: "less than 1 $singular ago";
if ($age == 1)
return $future
? "in 1 $singular"
: "1 $singular ago";
if ($age < $factor)
return $future
? "in $age $plural"
: "$age $plural ago";
$age = (int)($age/$factor);
}
}
Ricevo un ERRORE: "elenco" imprevisto (T_LIST). Che cosa sto facendo di sbagliato? – hozza
Qualcosa da fare con le versioni di PHP? Ho funzionato dichiarando la lista 'lista ($ singolare, $ plurale, $ fattore) = $ scala;' all'interno del foreach e sostituendo l'elenco nelle carrozzine di foreach con '$ list' – hozza
Questo è corretto. PHP 5.5 ha aggiunto "Disimballaggio degli array annidati con list()" (http://php.net/manual/en/control-structures.foreach.php) –
Non può esistere una funzione di inversione perché non si tratta di una biiezione. La stringa di origine da cui si ottiene un timestamp UNIX quando si utilizza strtotime
può essere formattata in molti modi diversi. Quindi, se decidi di invertire la funzione, come puoi sapere quale formato di stringa usare? Potrebbe essere il 2010-08-05 o il 10 settembre 2000, ecc. Questo è esattamente il motivo per cui non esiste una funzione di inversione, ma come ha giustamente detto Andypandy, devi usare date()
che ti permette di definire effettivamente il formato di stringa che desideri terminare con. So che questa domanda è vecchia, ma ho pensato che meritasse questa risposta, così gli altri utenti capiscono perché non esiste una tale funzione in PHP.
mentre tecnicamente "corretto" questo non risponde alla domanda dell'OP, piuttosto promuove solo uno stereotipo negativo del programmatore che conosce tutto, che conosce meglio del La persona che fa la domanda, un altro approccio sarebbe dire qualcosa di simile -> http://stackoverflow.com/a/3040437/830899 – unsynchronized
data ("Ymd", time()) può fare il lavoro – Qinjie
- 1. PHP Strtotime -1month -2month
- 2. PHP - strtotime - Aggiungi ore
- 3. PHP Data strtotime non funziona
- 4. PHP strtotime: Get mese precedente
- 5. Perché strtotime ('') e strtotime ('.') Restituiscono un timestamp?
- 6. forza php strtotime da usare UTC
- 7. Javascript equivalente di strtotime di php()?
- 8. PHP strtotime che restituisce sempre mezzanotte
- 9. PHP: strtotime() restituisce sempre 01/01/1970
- 10. strtotime con lingue diverse?
- 11. data strtotime strano risultato
- 12. python - equivalente a strtotime?
- 13. php strtotime() pasticcia con la data di un altro anno
- 14. PHP strtotime e JavaScript Date.parse restituiscono timestamp diversi
- 15. PHP strtotime() sembra che si aspetti un formato Euro
- 16. PHP: strtotime restituisce false per una data futura?
- 17. reverse mysql_real_escape_string
- 18. Reverse engineering?
- 19. reverse htmlspecialchars
- 20. Come faccio a strtotime in python?
- 21. Come ottenere con precisione l'ora UTC corrente tramite strtotime?
- 22. Perché il mio strtotime ("oggi") non funziona?
- 23. oggetto Reverse in jQuery.each
- 24. String reverse in Python
- 25. AngularJS: Reverse Checkbox State
- 26. reverse xdelta patch
- 27. reverse-step errore multithread
- 28. bindAttr reverse boolean
- 29. iOS Twitter Reverse OAuth
- 30. Reverse lista Scala
data(). Vedi http://php.net/manual/en/function.date.php –
@Andypandy: So di 'date()'. Volevo chiedere, c'è una funzione diretta che farà il contrario di 'strtotime'? – prongs
se non la data, non lo so ... potresti fornire qualche altro contesto? qualcosa del genere ($ timestring = date ('l', $ timestamp) non funziona? –