Datum in Unix-Timestamp konvertieren
Das folgende Beispiel zeigt, wie Sie ein Formular erstellen, in welches man ein beliebiges Datum eingeben und in einen UNIX-Timestamp konvertieren kann.
Demo
- Online-Demo öffnen
Quellcode Beispiel
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta name="description" content="" /> <meta name="keywords" content="" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Datum in Unix-Timestamp konvertieren</h1> <?php // Übergebene Werte in lokale PHP-Variablen speichern $i_day = (int) (isset($_GET['day']) ? $_GET['day'] : 1); $i_month = (int) (isset($_GET['month']) ? $_GET['month'] : 1); $i_year = (int) (isset($_GET['year']) ? $_GET['year'] : date('Y')); $i_hour = (int) (isset($_GET['hour']) ? $_GET['hour'] : 0); $i_minute = (int) (isset($_GET['minute']) ? $_GET['minute'] : 0); $i_second = (int) (isset($_GET['second']) ? $_GET['second'] : 0); ?> <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p> <label for="day">Tag:</label> <input type="text" name="day" id="day" size="2" maxlength="2" value="<?php echo $i_day; ?>" /> </p> <p> <label for="month">Monat:</label> <input type="text" name="month" id="month" size="2" maxlength="2" value="<?php echo $i_month; ?>" /> </p> <p> <label for="year">Jahr:</label> <input type="text" name="year" id="year" size="4" maxlength="4" value="<?php echo $i_year; ?>" /> </p> <p> <label for="hour">Stunde:</label> <input type="text" name="hour" id="hour" size="2" maxlength="2" value="<?php echo $i_hour; ?>" /> </p> <p> <label for="minute">Minute:</label> <input type="text" name="minute" id="minute" size="2" maxlength="2" value="<?php echo $i_minute; ?>" /> </p> <p> <label for="second">Sekunde:</label> <input type="text" name="second" id="second" size="2" maxlength="2" value="<?php echo $i_second; ?>" /> </p> <p> <input type="hidden" name="submit" value="1" /> <input type="submit" value="Konvertieren" /> </p> </form> <hr /> <?php // Wenn das Formular abgeschickt wurde... if (!empty($_GET['submit'])) { // ... dann aus den übermittelten Daten einen UNIX-Timestamp erzeugen. $i_timestamp = (int) mktime ($i_hour, $i_minute, $i_second, $i_month, $i_day, $i_year ); // Ausgabe echo 'Datum: ', date('d.m.Y H:i:s', $i_timestamp); // zur Kontrolle echo '<br />'; echo 'UNIX-Timestamp: ', $i_timestamp; } ?> </body> </html>