Forum und email

strtotime

(PHP 4, PHP 5)

strtotime — Μετατρέψτε σχεδόν οποιαδήποτε ημερομηνία ή ώρα που είναι σε μορφή Αγγλικού κειμένου σε ένα UNIX timestamp

Περιγραφή

int strtotime ( string $time [, int $now ] )

Η συνάρτηση περιμένει να της δοθεί ένα string που περιέχει μία ημερομηνία στα Αγγλικά και θα προσπαθήσει να τη μετατρέψει σε ένα UNIX timestamp σχετικό με αυτό που δίνεται με την παράμετρο now , ή την τρέχουσα ώρα εάν δε δοθεί κανένα. Σε περίπτωση αποτυχίας, ένα -1 επιστρέφεται.

Επειδή η strtotime() συμπεριφέρεατι σύμφωνα με το συντακτικό ημερομηνιών του GNU, ανατρέξτε στο manual του GNU στη σελίδα με τίτλο » Date Input Formats. Εκεί περιγράφεται η έγκυρη σύνταξη της παραμέτρουtime .

Example#1 Παραδείγματα της strtotime()

<?
echo strtotime ("now"), "\n";
echo 
strtotime ("10 September 2000"), "\n";
echo 
strtotime ("+1 day"), "\n";
echo 
strtotime ("+1 week"), "\n";
echo 
strtotime ("+1 week 2 days 4 hours 2 seconds"), "\n";
echo 
strtotime ("next Thursday"), "\n";
echo 
strtotime ("last Monday"), "\n";
?>

Example#2 Έλεγχος αποτυχίας

<?
$str 
'Not Good';
if ((
$timestamp strtotime($str)) === -1) {
    echo 
"The string ($str) is bogus";
} else {
    echo 
"$str == "date('l dS of F Y h:i:s A',$timestamp);
}
?>

Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)