PDA

View Full Version : Converting timestamp to 3 numeric fields ?


jeddi
08-09-2006, 03:18 PM
I have written some code to collect the date from three input fields and store it as a timestamp date format.

This is the code ;


echo "<div style='width:40px;position:absolute;left:400px; top:300px;' >
\n<select name=\"x_day\">\n";

for($day = 1; $day <= 31; $day++) {
if($day == $N_day) {
echo "<option value=\"$day\" selected= \"selected\" >$day</option>";
}
else {
echo "<option value=\"$day\">$day</option>";
}
}
echo "</select>\n</div>\n";

echo "<div style='width:40px;position:absolute;left:450px; top:300px;' >
\n<select name=\"x_month\">\n";

for($month = 1; $month <= 12; $month++) {
if($month == $N_month) {
echo "<option value=\"$month\" selected= \"selected\" >$month</option>";
}
else {
echo "<option value=\"$month\">$month</option>";
}
}
echo "</select>\n</div>\n";

echo "<div style='width:40px;position:absolute;left:500px; top:300px;' >
\n<select name=\"x_year\">\n";

for($year = 2006; $year <= 2007; $year++) {
if($year == $N_year) {
echo "<option value=\"$year\" selected= \"selected\" >$year</option>";
}
else {
echo "<option value=\"$year\">$year</option>";
}
}
echo "</select>\n</div>\n";



Just one problem - for editing the record I need to reverse the process!

A timestamp field from the database which looks like this:
1158440400

needs to be changed into the correct three variables namely:
$N_day, $N_month and $N_year which should be
something like 17,08,2006

Any ideas on how I do this ??

Thank you for help.

Fumigator
08-09-2006, 03:35 PM
If it's truly a timestamp data type, you can select it from the database using the date_format() function and format it however you wish.

http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html

If it's not an actual timestamp... see if you can make it one :)

jeddi
08-09-2006, 04:04 PM
It is saved in the muSql table as a numeric - int(12)

I dont know how to make it into "an actual timestamp" !

Any help would be much appreciated. :)

NancyJ
08-09-2006, 04:15 PM
post more of your code, you're only showing generation of your date form so far.
We have no idea how you're generating this 'timestamp' so we cant say if its a real timestamp of not.
Assuming it is a real unix timestamp you can do

$N_day = date("d", $timestamp);
$N_month = date("m", $timestamp);
$N_year = date("Y", $timestamp);

jeddi
08-09-2006, 04:54 PM
This is the code I am using to collect and create my timestamp:


$N_day = strip_tags(trim($_POST["x_day"]));
$N_month = strip_tags(trim($_POST["x_month"]));
$N_year = strip_tags(trim($_POST["x_year"]));

/*
* Create avent date from the three input fields.
*/

$N_ev_dat = strtotime(sprintf("%04d/%02d/%02d", $N_year, $N_month,$N_day));

jeddi
08-09-2006, 04:57 PM
Oh - and BTW I used the
sprintf( ) function without really undertsanding it :D

What does it do ? and should I be using it ?

NancyJ
08-09-2006, 05:03 PM
what you want to be doing is

$N_ev_dat = strtotime("$N_month/$N_day/$N_year");

you dont need the sprintf

jeddi
08-09-2006, 08:04 PM
OH, thanks ...

...but that bit works fine - I posted that bit of code b'cos I was asked to show how the timestamp was arrived at.


post more of your code, you're only showing generation of your date form so far.
We have no idea how you're generating this 'timestamp' so we cant say if its a real timestamp of not.

the bit I am having problems with is getting it back into three variables again so that I can show them for editing.

Is that idea given earlier still valid ?

i.e.

$N_day = date("d", $timestamp);
$N_month = date("m", $timestamp);
$N_year = date("Y", $timestamp);



In my case I guess it is:


$N_day = date("d", $N_ev_dat);
$N_month = date("m", $N_ev_dat);
$N_year = date("Y", $N_ev_dat);


Is that right ??

Thanks again for your help.

NancyJ
08-09-2006, 08:17 PM
in the code I posted $timestamp should be whatever you're calling the $timestamp when you pull it from the db

jeddi
08-09-2006, 08:26 PM
Yes - I get that.

Thanks for your help.