943,865 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 8924
  • MySQL RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Mar 2nd, 2009
0

Re: php/mysql display data in row and column

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
Reputation Points: 32
Solved Threads: 3
Posting Whiz in Training
MaxMumford is offline Offline
228 posts
since Oct 2006
Mar 2nd, 2009
0

Re: php/mysql display data in row and column

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.
Reputation Points: 32
Solved Threads: 3
Posting Whiz in Training
MaxMumford is offline Offline
228 posts
since Oct 2006
Mar 2nd, 2009
0

Re: php/mysql display data in row and column

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
HB25 is offline Offline
74 posts
since Dec 2007
Mar 2nd, 2009
0

Re: php/mysql display data in row and column

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 !
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 2nd, 2009
0

Re: php/mysql display data in row and column

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?
MySQL Syntax (Toggle Plain Text)
  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')";
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
HB25 is offline Offline
74 posts
since Dec 2007
Mar 2nd, 2009
0

Re: php/mysql display data in row and column

Hmm.. Okay! From what I have read so far, this is what STR_TO_DATE do.
mysql Syntax (Toggle Plain Text)
  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.
mysql Syntax (Toggle Plain Text)
  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 .
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 2nd, 2009
0

Re: php/mysql display data in row and column

Hi
So how can we apply this to my query if you think it’s going to work?
Regards
HB25
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
HB25 is offline Offline
74 posts
since Dec 2007
Mar 2nd, 2009
0

Re: php/mysql display data in row and column

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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 2nd, 2009
0

Re: php/mysql display data in row and column

Click to Expand / Collapse  Quote originally posted by HB25 ...
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 ?
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 2nd, 2009
0

Re: php/mysql display data in row and column

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
HB25 is offline Offline
74 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Create a trigger for doing a insert from a remote database/server
Next Thread in MySQL Forum Timeline: MySQL num rows error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC