Forum Moderators: coopster

Message Too Old, No Replies

Timestamp Display question

         

thx967

12:15 am on Feb 16, 2005 (gmt 0)

10+ Year Member



I have a timestamp field i would like to format as numeric mm/dd/yr. I've been combing this forum for insight but can't find anything. Can anyone help?

I'm using PHP Version 4.3.10 on a Linux box with Apache/1.3.29.

JohnCanyon

12:23 am on Feb 16, 2005 (gmt 0)

10+ Year Member



date("m/d/y",mktime());

thx967

12:29 am on Feb 16, 2005 (gmt 0)

10+ Year Member



so something like this:

echo 'date("m/d/y",mktime($row['datetime']))';

JohnCanyon

12:58 am on Feb 16, 2005 (gmt 0)

10+ Year Member



If your timestamp is a unix timestamp then all you need is:

date("m/d/y",$row[timestamp]);

thx967

1:10 am on Feb 16, 2005 (gmt 0)

10+ Year Member



I get a parse error with that code on the line that the mktime is on:

parse error, unexpected T_STRING, expecting ',' or ';'

Heres more of the code i'm displaying

echo '<table class="table" width="100%">';
echo '<tr>';
while ($row = $connector->fetchArray($result))
{

echo '<tr><td width="15">';
echo $row['ID'];
echo '</td>';
echo '<td width="45">';
echo 'date("m/d/y",mktime $row['datetime']';
echo '</td>';
echo '<td width="45">';
echo $row['firstname']," ", $row['lastname'];
echo '</td>';
echo '<td width="45">';
echo $row['email'];
echo '</td>';

echo '</td></tr>';
}
echo '</table>';

coopster

1:14 am on Feb 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You don't want to have quotes around your date function like that. Try this:

echo date("m/d/y",mktime $row['datetime'];

On another note, you could also use the database-specific Date/Time functions to format during SELECTion rather than format afterward using PHP. For example, in MySQL you could ...

SELECT DATE_FORMAT(timestamp_field, '%m/%d/%y') FROM table;

thx967

1:39 am on Feb 16, 2005 (gmt 0)

10+ Year Member



still get a parse error without the quotes:

parse error, unexpected T_VARIABLE

Doesn't the date function need a $ sign before it?
i.e. - echo $date("m/d/y",mktime $row['datetime']);

here is the edited code i got the parse error with:

echo '<table class="table" width="100%">';
echo '<tr>';
while ($row = $connector->fetchArray($result))
{

echo '<tr><td width="15">';
echo $row['ID'];
echo '</td>';
echo '<td width="45">';
echo date("m/d/y",mktime $row['datetime']);
echo '</td>';
echo '<td width="45">';
echo $row['firstname']," ", $row['lastname'];
echo '</td>';
echo '<td width="45">';
echo $row['email'];
echo '</td>';

echo '</td></tr>';
}
echo '</table>';

Changing the format in mysql didn't work either - and it may be useful to display the date and time in that field sometime down the road - so that might not be the best solution for me.

coopster

2:12 am on Feb 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to have a closer look at how the date() [php.net] and mktime() [php.net] functions work. Also, if your date field is indeed a timestamp, you are probably better off using the database method mentioned earlier.


Changing the format in mysql didn't work either -

Yes, it will. You must have something else incorrect. Even if you do think that it may be useful to display the date and time in that field sometime down the road, you can still return that field as well. You just select it as is as well as the formatted version. Something like

SELECT timestamp_field, DATE_FORMAT(timestamp_field, '%m/%d/%y') AS mmddyy FROM table;

dmmh

9:49 am on Feb 16, 2005 (gmt 0)

10+ Year Member




function func_change_timestamp($svDate, $svDateOutput)
{
$year = substr($svDate,0,4);
$month = substr($svDate,4,2);
$day = substr($svDate,6,2);
$hour = substr($svDate,8,2);
$minute= substr($svDate,10,2);
$sec = substr($svDate,12,2);

$svDateOutput = ereg_replace ("YYYY", $year, $svDateOutput);
$svDateOutput = ereg_replace ("MM", $month, $svDateOutput);
$svDateOutput = ereg_replace ("DD", $day, $svDateOutput);
$svDateOutput = ereg_replace ("hh", $hour, $svDateOutput);
$svDateOutput = ereg_replace ("mm", $minute, $svDateOutput);
$svDateOutput = ereg_replace ("ss", $sec, $svDateOutput);

echo $svDateOutput;
};

call it like:
<? $date_output = 'MM-DD-YYYY'; func_change_timestamp($row['timestamp_field'], $date_output);?>

thx967

6:38 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



thanks guys!

The solution dmmh posted works - with the exception that it prints the mm-dd-yyyy before the date.

Heres the output - MM-DD-YY02-15-2005

code - echo $date_output = 'MM-DD-YY'; func_change_timestamp($row['datetime'], $date_output);

dmmh

8:17 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



dont use echo $dateoutput, the function echoes out by itself, so if you specify the format to display the timestamp ($date_output = 'MM-DD-YYYY';) and next call the fucntion and pass it the format, it will already echo out by itself

see the function ;)
if you want to your code to stay like it is now, change the 'echo $svDateOutput;' inside the function to 'return $svDateOutput;'

just use it like this:

<? $date_output = 'MM-DD-YYYY'; //dont know if 'MM-DD-YY' works
func_change_timestamp($row['timestamp_field'], $date_output);?>

thx967

8:35 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



Thanks! that worked

dmmh

9:23 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



welcome, timestamps are a #*$! imo :)

I have something similar for datetime fileds to if you need btw

thx967

9:33 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



sure - can you post it here? could use all the help I can get with these bloody dates.

btw - is there an easy way to shorten the yyyy to just the last 2 digits yy?

dmmh

10:36 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



sure, you can do that like this:

function func_change_timestamp($svDate, $svDateOutput)
{
$year = substr($svDate,2,4);
$month = substr($svDate,4,2);
$day = substr($svDate,6,2);
$hour = substr($svDate,8,2);
$minute= substr($svDate,10,2);
$sec = substr($svDate,12,2);

$svDateOutput = ereg_replace ("YY", $year, $svDateOutput);
$svDateOutput = ereg_replace ("MM", $month, $svDateOutput);
$svDateOutput = ereg_replace ("DD", $day, $svDateOutput);
$svDateOutput = ereg_replace ("hh", $hour, $svDateOutput);
$svDateOutput = ereg_replace ("mm", $minute, $svDateOutput);
$svDateOutput = ereg_replace ("ss", $sec, $svDateOutput);

echo $svDateOutput;
};

next use:

<? $date_output = 'MM-DD-YY'; func_change_timestamp($row['timestamp_field'], $date_output);?>

dmmh

10:37 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



for the datetime:

function func_change_datetime($svDate, $svDateOutput)
{
$year = substr($svDate,0,4);
$month = substr($svDate,5,2);
$day = substr($svDate,8,2);
$hour = substr($svDate,11,2);
$minute= substr($svDate,14,2);
$sec = substr($svDate,17,2);

$svDateOutput = str_replace ("YYYY", $year, $svDateOutput);
$svDateOutput = str_replace ("MM", $month, $svDateOutput);
$svDateOutput = str_replace ("DD", $day, $svDateOutput);
$svDateOutput = str_replace ("hh", $hour, $svDateOutput);
$svDateOutput = str_replace ("mm", $minute, $svDateOutput);
$svDateOutput = str_replace ("ss", $sec, $svDateOutput);

echo $svDateOutput;
};

thx967

10:57 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



changing the $year = substr($svDate,0,4);

to
$year = substr($svDate,2,4);

didn't leave me with a 2 digit date - still have 4 digits with a wacky year - lol

02-15-0502

dmmh

5:02 am on Feb 17, 2005 (gmt 0)

10+ Year Member



dude, have you even looked at the code?

just look at the code. Its not like I dont wanne help, but getting al your questions anwswered isnt gonna help you much in the long run.

inside the functions are 2 php default fucntions:

substr() [nl3.php.net]

and

ereg_replace() [nl3.php.net]

take a look at what they actually do.

function func_change_timestamp($svDate, $svDateOutput)

$svDate is your date string (timestamp), $svDateoutput is the format we want it displayed in.

thats all you're gonna get from me this time ;)

its so easy, just take the time