Home > Archive > PHP Language > May 2006 > Get 'year', 'month' etc from Date
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Get 'year', 'month' etc from Date
|
|
| bettina@coaster.ch 2006-05-13, 6:57 pm |
| Hallo,
In meiner Datenbank habe ich ein Feld DATE (Typ Datetime).
Ich m=F6chte den Inhalt dieses Feldes holen und das Jahr und Monat
separat anzeigen.
Ich habe es probiert mit Folgendes und funktioniert:
$today =3D getdate();
$today_year =3D $today['year'];
echo $today_year;
Aber wenn ich das Datum aus der Datenbank hole und versuche das Jahr
und Monat separat anzuzeigen, das DAtum wird richtig angezeigt, aber
f=FCr das Jahr und f=FCr den Monat wird den Wert "2" angezeigt. Hier ist
der Code:
$get_date =3D mysql_query("SELECT DATE FROM meetings WHERE ....... ");
$date =3D mysql_result($get_date,0,0);
echo $date;
..=2E.
$mon =3D $date['mon'];
$year =3D $date['year'];
..=2E.
echo $mon;
..=2E..
echo $year;
So wird es angezeigt:
2006-05-18 08:00:00
2
2=20
------------
Was mache ich falsch?????
Danke im Voraus. Bettina
| |
| Janwillem Borleffs 2006-05-13, 6:57 pm |
| bettina@coaster.ch wrote:
> So wird es angezeigt:
>
> 2006-05-18 08:00:00
>
> 2
>
> 2
>
$date is a string, of which the first character is a '2'. This character has
a position of 0 in de string.
When you tread a string as an array, the character at the indicated position
is returned. Note that although this behaviour is still supported in PHP, it
has been marked as deprecated for a while now.
Because you are using a named index (not an integer, but a string), it gets
evaluated to an integer. And since 'year' and 'mon' evaluate to 0 when
casted to an integer, you are getting the same result as with substr($date,
0, 1) or $date{0}.
The simplest way to achieve what you want is to apply the strftime and
getdate functions:
$date = getdate(strtotime($date));
JW
| |
| bettina@coaster.ch 2006-05-13, 6:57 pm |
| Danke!
| |
| Michael Fesser 2006-05-14, 6:58 pm |
| ..oO(bettina@coaster.ch)
>In meiner Datenbank habe ich ein Feld DATE (Typ Datetime).
>Ich möchte den Inhalt dieses Feldes holen und das Jahr und Monat
>separat anzeigen.
Du kannst YEAR() und MONTH() benutzen, so daß die Datenbank die
gewünschten Werte direkt liefern kann.
BTW: Im deutschsprachigen Raum existieren <news:de.comp.lang.php.misc>
und <news:de.comp.datenbanken.mysql>.
Micha
|
|
|
|
|