Jump to content

Archived

This topic is now archived and is closed to further replies.

sun14php

Confusing Dates & Time

Recommended Posts

what should i do for following results ?

1. I want to enter date in textbox(form html/php) in dd-mm-yy format
2. the above date i enter in dd-mm-yy format should be stored in mysql date column either in yyyy-mm-dd or dd-mm-yy format.
3. date stored in mysql column should be displayed in browser in dd-mm-yy fromat.

i think i worte so simple. if yes help me out.

thanx a lot

Share this post


Link to post
Share on other sites
I shall make a start for you...
[b]Note: You will need to add validation and error checking, this serves as example only.[/b]

Adding:
[code]<?php
$strDate = $_POST["my_date_box"];
$arrayDate = explode("-", $strDate);

$intTimestamp = mktime(0, 0, 0, $arrayDate[1], $arrayDate[0], $arrayDate[2]);

//** add timestamp to a MySQL.int column
?>[/code]

We split the string up, and turn it into a UNIX-timestamp (using mktime()), this way we can manage it easier.

Displaing
[code]<?php
$intTimestamp = 0; // The timestamp from your MySQL.int column
echo date("d-m-y", $intTimestamp);
?>[/code]

After getting the timestamp from your SQL db, we can manage how it is displayed by using the date() function.

hth, Zac.

Share this post


Link to post
Share on other sites
i used ur code but instead of storing 02-06-06 it's storing 0000-00-00 in mysql database. why ?
in browser it's showing : 30-11-1999
& what is the meaning of the line: //** add timestamp to a MySQL.int column

Share this post


Link to post
Share on other sites
[!--quoteo(post=389173:date=Jun 29 2006, 09:26 AM:name=sun14php)--][div class=\'quotetop\']QUOTE(sun14php @ Jun 29 2006, 09:26 AM) [snapback]389173[/snapback][/div][div class=\'quotemain\'][!--quotec--]
i used ur code but instead of storing 02-06-06 it's storing 0000-00-00 in mysql database. why ?
in browser it's showing : 30-11-1999
& what is the meaning of the line: //** add timestamp to a MySQL.int column
[/quote]

$date=("dmy");

input name="date"

Share this post


Link to post
Share on other sites
[!--quoteo(post=389174:date=Jun 29 2006, 04:31 AM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Jun 29 2006, 04:31 AM) [snapback]389174[/snapback][/div][div class=\'quotemain\'][!--quotec--]
$date=("dmy");

input name="date"
[/quote]


this code didn't help too.

Share this post


Link to post
Share on other sites
[!--quoteo(post=389188:date=Jun 29 2006, 06:23 AM:name=sun14php)--][div class=\'quotetop\']QUOTE(sun14php @ Jun 29 2006, 06:23 AM) [snapback]389188[/snapback][/div][div class=\'quotemain\'][!--quotec--]
this code didn't help too.
[/quote]

according to your initial question, you're storing the date in the database as a DATE datatype, not an integer, so you'll need to format it as such. try something like this (and, as mentioned above, you'll have to work some error checking, as this just shows the principle:
[code]
// make sure that the user date is in DD-MM-YYYY format first

list($day, $month, $year) = explode("-", $_POST['userDate']);
$insertDate = "$year-$month-$day"; // insert this value into your table

// now, once you pull it back out of your table, just display it like this
$sql = mysql_query("SELECT * FROM tableName");
while ($x = mysql_fetch_array($sql)) {
  $date = date('d-m-Y', strtotime($x['dateColumn']));
  echo "$date<br />\n";
}
[/code]

good luck

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.