Forums

PHP

This topic is locked

Date Time Issue

Posted 08 Sep 2006 18:55:54
1
has voted
08 Sep 2006 18:55:54 Fouad Akhtar posted:
i want to display Date and Time

so far i could get to know about the following code for displaying date

<?php
$my_t=getdate(date("U");
$Val="$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]";
print("$Val";
?>

in the above code, i could only display Date which is "Friday, September 8, 2006", but unable to get a time

i need to display exact format Friday, September 8, 2006 with time alone with Am PM and need to insert to the datebase

my table is Dat
and field is DatTim with data type DateTime

i would highly appreciate your concern to this problem

Thanks in Advance

Replies

Replied 11 Sep 2006 10:40:08
11 Sep 2006 10:40:08 Patrick Woldberg replied:
Use the time() function to get the current time, you can use this value to insert in the database, to format the timestamp to display you use the date function, example:

<pre id=code><font face=courier size=2 id=code>// Return current Unix timestamp
$currentTime = time();
// Format time to display
$displayTime = date("l, F j, Y, g:i a";
// Print to screen
print($displayTime);</font id=code></pre id=code>

--------------------------------------------------
Patrick Woldberg
Web Developer at Dynamic Zones
Administrator at DMXzone.com, FLzone.net, FWzone.net and DNzone.com
--------------------------------------------------
Replied 11 Sep 2006 10:53:28
11 Sep 2006 10:53:28 Patrick Woldberg replied:
Forgot that you have to convert the value in the database also if you want to use a DateTime field in the database, I often simply use a integer field for it. You can convert it in MySQL with the FROM_UNIXTIME() function and convert it back to a unixtime with UNIX_TIMESTAMP().

Sample of a select:

$query = "SELECT UNIX_TIMESTAMP(DatTim) AS Time FROM Dat";

Sample of an insert:

$query = "INSERT INTO Dat SET DatTim = FROM_UNIXTIME($currentTime)";

--------------------------------------------------
Patrick Woldberg
Web Developer at Dynamic Zones
Administrator at DMXzone.com, FLzone.net, FWzone.net and DNzone.com
--------------------------------------------------
Replied 13 Sep 2006 14:51:53
13 Sep 2006 14:51:53 Fouad Akhtar replied:

brother you have done awesome work for me ... i was suffering with this issue from many many DAYs

i have only 1 more same question .....

since after implementing!

$query = "SELECT UNIX_TIMESTAMP(DatTim) AS Time FROM Dat";

Sample of an insert:

$query = "INSERT INTO Dat SET DatTim = FROM_UNIXTIME($currentTime)";

it is storing values in the database with the format of "2006-09-13 14:08:44" ... now my question is,

how can i change 2006-09-13 14:08:44 to Wednesday, September 13, 2006, 2:40 pm for viewing on the website ..... i mean it should select datetime from the database but display different on the website as mentioned Wednesday, September 13, 2006, 2:40 pm .


Replied 13 Sep 2006 16:52:54
13 Sep 2006 16:52:54 Roddy Dairion replied:
$query = "SELECT date_format(DatTim, '%W, %M %d, %Y, %I:%s %p') AS Time FROM Dat";
Replied 14 Sep 2006 10:55:17
14 Sep 2006 10:55:17 Patrick Woldberg replied:
print(date("l, F j, Y, g:i a", $query['DatTim']));

--------------------------------------------------
Patrick Woldberg
Web Developer at Dynamic Zones
Administrator at DMXzone.com, FLzone.net, FWzone.net and DNzone.com
--------------------------------------------------
Replied 25 Sep 2006 23:21:09
25 Sep 2006 23:21:09 Fouad Akhtar replied:
its done
thanks <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

Reply to this topic