php/mysql display data in row and column

Thread Solved

Join Date: Oct 2006
Posts: 200
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 2
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Posting Whiz in Training

Re: php/mysql display data in row and column

 
0
  #21
Mar 2nd, 2009
I think generally to store dates into the database its best to use a timestamp. A timestamp shows the number of seconds that have passed since January 1, 1970 00:00:00 GMT. You get the current timestamp in php by using:


time(); //http://uk.php.net/manual/en/function.time.php

You can convert a date into a timestamp using the following:

mktime('','','',$month,$day,$year); //http://uk.php.net/manual/en/function.mktime.php

And you can convert timestamp into a date by using this:

date('D-M-Y',$timestamp);

Using these functions, get the user to enter in their date in the format you want (DD/MM/YY) using 3 different text boxes like this:

<form action="process.php" method="post">
<input type="text" name="day" value="day"/><br />
<input type="text" name="month" value="month"/><br />
<input type="text" name="year" value="year"/><br />
<input type="submit" /><br />
</form>

then on the next page process it like this:

<?php
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];

$timestamp = mktime('','','',$month,$day,$year);

//then store the timestamp into the database 
?>

Then later if you want to get the timestamp from the database do something like this:

<?php
$get_date = mysql_query('SELECT timestamp FROM table WHERE bookingID = "'.$bookingID.'"');
$timestamp = mysql_fetch_array($get_date);
$timestamp = $date['timestamp'];

$timestamp = date('D-M-Y',$timestamp);
//$timestamp is now a readable version of the timestamp date in the database
?>


the good thing about using timestamps is that if you want to order the items in your database by date you can do it easily. Also if you want to get bookings within the last month, do something like this:

$now = time();
$oneMonthInSeconds = 2629743;
$lastmonth = $now - $oneMonthInSeconds;

$query = 'SELECT * FROM table WHERE timestamp > "'.$lastmonth.'" ';

And no problem about the help - iv had so much help from people like "nav33n" from this forum over the years, its good to give something back. I hope what im saying isnt too complicated - I get a bit excited xD
Ill solve somebody's thread someday! xD
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 200
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 2
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Posting Whiz in Training

Re: php/mysql display data in row and column

 
0
  #22
Mar 2nd, 2009
Sure,

$sql="INSERT INTO bookings 
(clientID, roomID, startdate, enddate, adults, children, roomtype,  requirements) 
VALUES 
('$last_insert_client_id',
'NULL',
'  str_to_date($_POST[startdate],'%d-%m-%Y')',
'  str_to_date($_POST[enddate],'%d-%m-%Y')',
'$_POST[adults]',
'$_POST[children]',
'$_POST[roomtype]', 
'$_POST[requirements]')
";

That should give you what you want.
Ill solve somebody's thread someday! xD
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 74
Reputation: HB25 is an unknown quantity at this point 
Solved Threads: 0
HB25 HB25 is offline Offline
Junior Poster in Training

Re: php/mysql display data in row and column

 
0
  #23
Mar 2nd, 2009
Hi
This is what I get:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%m-%Y')','str_to_date(05/04/2009,'%d-%m-%Y')','1', '0','Single'

Any more advice?
Thanks
HB25
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: php/mysql display data in row and column

 
0
  #24
Mar 2nd, 2009
echo your query. Execute it in phpmyadmin. See what's the exact error.
P.S. I tried to work with str_to_date and I simply can't get it to work !
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 74
Reputation: HB25 is an unknown quantity at this point 
Solved Threads: 0
HB25 HB25 is offline Offline
Junior Poster in Training

Re: php/mysql display data in row and column

 
0
  #25
Mar 2nd, 2009
Hi
I have opened this thread in another form and this is what I have been giving as a code but it will return an error saying (Error: Column 'startdate' cannot be null) any advice?
  1. $startdate=mysql_real_escape_string($_POST[startdate]);
  2. $enddate=mysql_real_escape_string($_POST[enddate]);
  3. $adults=mysql_real_escape_string($_POST[adults]);
  4. $children=mysql_real_escape_string($_POST[children]);
  5. $roomtype=mysql_real_escape_string($_POST[roomtype]);
  6. $requirements=mysql_real_escape_string($_POST[requirements]);
  7.  
  8. $sql="INSERT INTO bookings (clientID, roomID, startdate, enddate, adults, children, roomtype, requirements) VALUES ('$last_insert_client_id','NULL',str_to_date('$startdate','%d-%m-%Y'),str_to_date('$enddate','%d-%m-%Y'),'$adults','$children','$roomtype','$requirements')";
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: php/mysql display data in row and column

 
0
  #26
Mar 2nd, 2009
Hmm.. Okay! From what I have read so far, this is what STR_TO_DATE do.
  1. STR_TO_DATE(date_string, FORMAT);
STR_TO_DATE function will format string and insert it to the date column. The second parameter 'format' is just to let mysql know in what format is the first parameter 'date_string' is in.
  1. INSERT INTO table123 (reg_date) VALUES (STR_TO_DATE('31/2004/04', '%d/%Y/%m'))
In the above example, date_string is 31/2004/04. I am asking mysql to insert this value to the table and the format in which I have given the date is, %d/%Y/%m. ie., date/Year/month .
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 74
Reputation: HB25 is an unknown quantity at this point 
Solved Threads: 0
HB25 HB25 is offline Offline
Junior Poster in Training

Re: php/mysql display data in row and column

 
0
  #27
Mar 2nd, 2009
Hi
So how can we apply this to my query if you think it’s going to work?
Regards
HB25
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: php/mysql display data in row and column

 
0
  #28
Mar 2nd, 2009
My suggestion is, store date in YYYY-mm-dd format itself. You can change the format using php's date function. As maxmumford has already mentioned, you can convert that date to unixtimestamp and use it in whatever format you want.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: php/mysql display data in row and column

 
0
  #29
Mar 2nd, 2009
Originally Posted by HB25 View Post
Hi
So how can we apply this to my query if you think it’s going to work?
Regards
HB25
It wont. You have to save it in varchar datatype, which again would cause you problems. For example, you can't sort the fields on date. You can't calculate the difference (or add) x number of days to this date field , etc.
Why do you want to store it in that format anyway ?
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 74
Reputation: HB25 is an unknown quantity at this point 
Solved Threads: 0
HB25 HB25 is offline Offline
Junior Poster in Training

Re: php/mysql display data in row and column

 
0
  #30
Mar 2nd, 2009
As i live in the UK and the format for date is like this.
I think i will go with your advice i shoud leve it as its.

BTW MaxMumford has solved my problem this time how i could mark him as a solver for my thread?

Once again thank you for all your kind help.

Best wishes
HB 25
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC