Datum: Tag zweistellig anzeigen ($day) -> statt 1 --> 01

pajas7

Grünschnabel
Hallo Zusammen,

Ich habe den folgenden Code:
PHP:
$day = substr($row['date'], 8, 2);
$day = $day - 0;

Der Tag wird jetzt immer so dargestellt: 1.12.2014
Wie kann ich sicherstellen das auch der Tag, zweistellig angezeigt wird: 01.12.2014?

Vielen Dank!
 
Zuletzt bearbeitet von einem Moderator:
Hi

Wenn der Zahlenwert <10 ist eine 0 dranhängen (mit '0'.bla )?
Der gezeigte Code passt nur irgendwie gar nicht zur Frage und macht auch nichts wirklich sinnvolles.
 
Zuletzt bearbeitet:
Das ist die Auswahl:
HTML:
<select name="day" id="day">
<option value="01" <?php if ($day == "01") {echo "selected";} ?>>01
<option value="02" <?php if ($day == "02") {echo "selected";} ?>>02
<option value="03" <?php if ($day == "03") {echo "selected";} ?>>03
<option value="04" <?php if ($day == "04") {echo "selected";} ?>>04
<option value="05" <?php if ($day == "05") {echo "selected";} ?>>05
<option value="06" <?php if ($day == "06") {echo "selected";} ?>>06
<option value="07" <?php if ($day == "07") {echo "selected";} ?>>07
<option value="08" <?php if ($day == "08") {echo "selected";} ?>>08
<option value="09" <?php if ($day == "09") {echo "selected";} ?>>09
<option <?php if ($day == "10") {echo "selected";} ?>>10
<option <?php if ($day == "11") {echo "selected";} ?>>11
<option <?php if ($day == "12") {echo "selected";} ?>>12
<option <?php if ($day == "13") {echo "selected";} ?>>13
<option <?php if ($day == "14") {echo "selected";} ?>>14
<option <?php if ($day == "15") {echo "selected";} ?>>15
<option <?php if ($day == "16") {echo "selected";} ?>>16
<option <?php if ($day == "17") {echo "selected";} ?>>17
<option <?php if ($day == "18") {echo "selected";} ?>>18
<option <?php if ($day == "19") {echo "selected";} ?>>19
<option <?php if ($day == "20") {echo "selected";} ?>>20
<option <?php if ($day == "21") {echo "selected";} ?>>21
<option <?php if ($day == "22") {echo "selected";} ?>>22
<option <?php if ($day == "23") {echo "selected";} ?>>23
<option <?php if ($day == "24") {echo "selected";} ?>>24
<option <?php if ($day == "25") {echo "selected";} ?>>25
<option <?php if ($day == "26") {echo "selected";} ?>>26
<option <?php if ($day == "27") {echo "selected";} ?>>27
<option <?php if ($day == "28") {echo "selected";} ?>>28
<option <?php if ($day == "29") {echo "selected";} ?>>29
<option <?php if ($day == "30") {echo "selected";} ?>>30
<option <?php if ($day == "31") {echo "selected";} ?>>31
</select>

Das hab ich auch noch gefunden:
PHP:
$day =  substr($date, 8, 2);


oder:
PHP:
$date = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'];

Ich bin ziehmlich RATLOS :-(
 
Zuletzt bearbeitet von einem Moderator:
Bevor Du das so im aktiven Gebrauch verwendest, normalisiere ich Dir das mal lieber:
PHP:
<?
$day = (int) substr( $row[ "date" ], 8, 2 );

$format = function ( $i ) {
  return ( $i < 10 ) ? "0" . $i : $i;
};
?>

<select name="date[day]" id="select-day">
<? for ( $i = 1; $i < 32; ++$i ): ?>
<? if ( $day === $i ): ?>
  <option selected><?= $format( $i ) ?></option>
<? else: ?>
  <option><?= $format( $i ) ?></option>
<? endif; ?>
<? endfor; ?>
</select>
 
Ja genau, das Datum kommt aus einer MySQL Datenbank. Sorry das ich das nicht erwähnt habe :confused:. Es handelt sich um eine Eventkalender.
 
Wie kommen die Einträge in die Datenbank,
warum musst Du strings zerschnippseln und
wieso wird das Datum nicht gleich universell zB als timestamp gespeichert?

(zum Thema)
Wenn Du Zugriff auf $day hast, könntest Du diese Variable auf Länge überprüfen und eine Null vorhängen wenn Länge==1.
PHP:
if( strlen($day) == 1 )
{
   $day = "0".$day;
}
// oder als Zahl abfragen
if( $day <10 )
{
   $day = "0".$day;
}
 
nix geht mehr....

Hier mal das komplette script:

PHP:
<body>

<?php
include 'config.php';

// AUTHENTICATION
if (isset($_POST['pw']))
{
$pw = $_POST['pw'];
$_SESSION['pw'] = $pw;
}
elseif (isset($_GET['pw']))
{
$pw = $_GET['pw'];
$_SESSION['pw'] = $pw;
}
elseif (isset($_SESSION['pw']))
{
$pw = $_SESSION['pw'];
}

if ($pw == $adminpass)
{
include 'library/opendb.php';
include 'library/timeFunctions.php';
include 'library/displayFunctions.php';

// get variables from form post for which record to change, and what to do with it
$action = $_GET['action'];
$id = $_GET['id'];


// THE DELETE OPTION
if($action == 'delete')
{
// set up delete query
$query = "DELETE FROM events WHERE id =" . $id;
// do the query
mysql_query($query) or die('Error, query failed');

echo "Anlass erfolgreich gel&ouml;scht!<br><br><a href='admin.php'>Neuer Anlass hinzuf&uuml;gen</a>";
}

// THE ADD OR EDIT OPTION
elseif (isset($_POST['add']) || isset($_POST['edit']))
{
// get date from form post
$date = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'];
// get time from form post
$rawTime = $_POST['time'];
// convert time to MySQL format
if($rawTime > 23 )
    {
    $rawTime = '0';
    }
$time = ($rawTime +  $_POST['ampm']) . ':' . $_POST['minute'] . ':00';
// get showTime
$showTime = $_POST['showTime'];
// showTime to 1 or 0
if ($showTime == "on")
{
$showTime = 1;
}
else
{
$showtime = 0;
}
// get venue
$venue = $_POST['venue'];
// get description
$description = $_POST['description'];
// set up insert query
if (isset($_POST['add'])) {
$query = "INSERT INTO events (date, time, showTime, venue, description) VALUES ('$date', '$time', '$showTime', '$venue', '$description')";
}
// set up update query
elseif (isset($_POST['edit'])) {
$query = "UPDATE events SET date = '$date', time = '$time', showTime = '$showTime', venue = '$venue', description = '$description' WHERE id ='$id'";
}
// do the query
mysql_query($query) or die('Error, insert query failed');

echo "Liste erfolgreich aktualisiert!<br><br><a href='admin.php'>Neuer Anlass hinzuf&uuml;gen</a>";
}




// ADD/EDIT FORM
else
{
    // load stored values for editing event
    if($action == 'edit')
    {
        $query = "SELECT * FROM events WHERE id='$id'";
        $result = mysql_query($query);
        $date = mysql_result($result,0,"date");
        $time = mysql_result($result,0,"time");
        $venue = mysql_result($result,0,"venue");
        $description = mysql_result($result,0,"description");
        $month =  substr($date, 5, 2);
        $day =  substr($date, 8, 2);
        $format = function ( $i ) {
        $year =  substr($date, 0, 4);
        $hour = realHour($time);
        $minute = substr($time, 3, 2);
        $ampm = realAMPM($time);
        $showTime = mysql_result($result,0,"showTime");
    }


?>
<form method="post">
<table width="400" border="0" cellspacing="1" cellpadding="2">

<tr>
<td width="100">Datum</td>
<td>

<select name="month" id="month">
<option value="01" <?php if ($month == "01") {echo "selected";} ?>>Jan
<option value="02" <?php if ($month == "02") {echo "selected";} ?>>Feb
<option value="03" <?php if ($month == "03") {echo "selected";} ?>>Mar
<option value="04" <?php if ($month == "04") {echo "selected";} ?>>Apr
<option value="05" <?php if ($month == "05") {echo "selected";} ?>>Mai
<option value="06" <?php if ($month == "06") {echo "selected";} ?>>Jun
<option value="07" <?php if ($month == "07") {echo "selected";} ?>>Jul
<option value="08" <?php if ($month == "08") {echo "selected";} ?>>Aug
<option value="09" <?php if ($month == "09") {echo "selected";} ?>>Sep
<option value="10" <?php if ($month == "10") {echo "selected";} ?>>Okt
<option value="11" <?php if ($month == "11") {echo "selected";} ?>>Nov
<option value="12" <?php if ($month == "12") {echo "selected";} ?>>Dez
</select>

<select name="day" id="day">
<option value="01" <?php if ($day == "01") {echo "selected";} ?>>01
<option value="02" <?php if ($day == "02") {echo "selected";} ?>>02
<option value="03" <?php if ($day == "03") {echo "selected";} ?>>03
<option value="04" <?php if ($day == "04") {echo "selected";} ?>>04
<option value="05" <?php if ($day == "05") {echo "selected";} ?>>05
<option value="06" <?php if ($day == "06") {echo "selected";} ?>>06
<option value="07" <?php if ($day == "07") {echo "selected";} ?>>07
<option value="08" <?php if ($day == "08") {echo "selected";} ?>>08
<option value="09" <?php if ($day == "09") {echo "selected";} ?>>09
<option <?php if ($day == "10") {echo "selected";} ?>>10
<option <?php if ($day == "11") {echo "selected";} ?>>11
<option <?php if ($day == "12") {echo "selected";} ?>>12
<option <?php if ($day == "13") {echo "selected";} ?>>13
<option <?php if ($day == "14") {echo "selected";} ?>>14
<option <?php if ($day == "15") {echo "selected";} ?>>15
<option <?php if ($day == "16") {echo "selected";} ?>>16
<option <?php if ($day == "17") {echo "selected";} ?>>17
<option <?php if ($day == "18") {echo "selected";} ?>>18
<option <?php if ($day == "19") {echo "selected";} ?>>19
<option <?php if ($day == "20") {echo "selected";} ?>>20
<option <?php if ($day == "21") {echo "selected";} ?>>21
<option <?php if ($day == "22") {echo "selected";} ?>>22
<option <?php if ($day == "23") {echo "selected";} ?>>23
<option <?php if ($day == "24") {echo "selected";} ?>>24
<option <?php if ($day == "25") {echo "selected";} ?>>25
<option <?php if ($day == "26") {echo "selected";} ?>>26
<option <?php if ($day == "27") {echo "selected";} ?>>27
<option <?php if ($day == "28") {echo "selected";} ?>>28
<option <?php if ($day == "29") {echo "selected";} ?>>29
<option <?php if ($day == "30") {echo "selected";} ?>>30
<option <?php if ($day == "31") {echo "selected";} ?>>31
</select>
,
<select name="year" id="year">
<option <?php if ($year == (date('Y') - 3)) {echo "selected";} echo ">" . (date('Y') - 3); ?>
<option <?php if ($year == (date('Y') - 2)) {echo "selected";} echo ">" . (date('Y') - 2); ?>
<option <?php if ($year == (date('Y') - 1)) {echo "selected";} echo ">" . (date('Y') - 1); ?>
<option <?php if (($year == date('Y')) OR (!isset($year))) {echo "selected";}  echo ">" . date('Y'); ?>
<option <?php if ($year == (date('Y') + 1)) {echo "selected";} echo ">" . (date('Y') + 1); ?>
<option <?php if ($year == (date('Y') + 2)) {echo "selected";} echo ">" . (date('Y') + 2); ?>
<option <?php if ($year == (date('Y') + 3)) {echo "selected";} echo ">" . (date('Y') + 3); ?>
<option <?php if ($year == (date('Y') + 4)) {echo "selected";} echo ">" . (date('Y') + 4); ?>
</select>

</td>
</tr>
<tr>
<td width="100">Zeit</td>
<td>
<input name="time" type="text" id="time" size="2" maxlength="2" <?php if (isset($hour)) {echo "value=" . $hour;} else {echo "value='18'";} ?>> :
<input name="minute" type="text" id="minute" size="2" maxlength="2" <?php if (isset($minute)) {echo "value=" . $minute;} else {echo "value='00'";} ?>>
<select name="ampm" id="ampm" style="visibility:hidden;">
<option value="12" <?php if ($ampm == "PM") {echo "selected";} ?>>PM
<option value="0" <?php if ($ampm == "AM") {echo "selected";} ?>>AM
</select>
(Zeit anzeigen? <input name="showTime" type="checkbox" id="showTime" <?php if ($showTime) {echo "checked";} elseif (!isset($showTime)) {echo "checked";} ?>>)
</td>
</tr>
<tr>
<td width="100">Anlass</td>
<td>
<textarea name="venue" id="venue" rows="5" cols="80"><?php if (isset($venue)) {echo $venue;} ?></textarea>
</td>
</tr>
<tr>
<td width="100">Ort</td>
<td>
<textarea name="description" id="description" rows="5" cols="80"><?php if (isset($description)) {echo $description;} ?></textarea>
</td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td>
<?php

if ($action == 'edit')
{
echo "<input name='edit' type='submit' id='edit' value='Anlass aktualisieren'>";
}
else
{
echo "<input name='add' type='submit' id='add' value='Anlass hinzuf&uuml;gen'>";
}
?>
</td>
</tr>
</table>
</form>
<?php
}
?>
<table border="0" cellspacing="3">
<tr>
<td colspan="9" align="center" class="highlight">
Bevorstehende Termine
</td>
</tr>
<?php

$query  = "SELECT * FROM events ORDER BY date, time";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
    if ($row['date'] >= date('Y-m-d'))
    {
displayEvents($row);
displayActions($row);
    }
}


?>
</table>
<BR>
<BR>
<table border="0" cellspacing="3">
<tr>
<td colspan="9" align="center" class="highlight">
Vergangene Termine
</td>
</tr>
<?php


$query  = "SELECT * FROM events ORDER BY date DESC, time DESC";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
    if ($row['date'] <= date('Y-m-d'))
    {
displayEvents($row);
displayActions($row);
    }
}


include 'library/closedb.php';
}

// AUTHENTICATION FAILED
  else
{
   echo "Wrong password";
}
?>
</table>
<br />


und hier das displayfunctions.php


PHP:
<?php




function displayEvents($row)
{
$day = substr($row['date'], 8, 2);
$day = $day - 0;
$month = substr($row['date'], 5, 2);
$year = substr($row['date'], 0, 4);
/*$dayw =  date("D", mktime(0, 0, 0, $month, $day, $year));*/
$monthName = date("M", mktime(0, 0, 0, $month, $day, $year));
$hour = realHour($row['time']);
$minute = substr($row['time'], 3, 2);
$ampm = realAMPM($row['time']);
$showTime = $row['showTime'];



$weekdays = array(
  "So",
  "Mo",
  "Di",
  "Mi",
  "Do",
  "Fr",
  "Sa"
);
$day_index = date( "w", mktime( 0, 0, 0, $month, $day, $year ) );
$dayw       = $weekdays[ $day_index ];


    echo "<tr><td valign='top' align='right' class='date'>$dayw,</td>" .
         "<td valign='top' align='right' class='dayw'>$day.$month.$year</td>" .
         "<td valign='top' align='right' class='time'>";
        if ($showTime) {echo  "$hour:$minute $ampm";}
    echo "</td>" .
         "<td class='spacer'>&nbsp;</td>" .
         nl2br("<td valign='top' class='venue'>{$row['venue']}</td>") .
         "<td class='spacer'>&nbsp;</td>" .
         nl2br("<td valign='top' class='description'>{$row['description']}</td></tr><tr><td colspan='9' height='5px'><hr noshade size='1'></td></tr>");

}
function displayActions($row)
{
         echo "<td valign='top'><a href='admin.php?id={$row['id']}&action=edit'>Editieren &uarr;</a></td>" .
         "<td valign='top'><a href='admin.php?id={$row['id']}&action=delete'>L&ouml;schen &uarr;</a></td></tr>" .
         "<tr><td colspan='9' height='0px'></td></tr>";
}


?>
 
Zuletzt bearbeitet von einem Moderator:
Zurück