Jump to content


Photo

Confusing Dates & Time


  • Please log in to reply
5 replies to this topic

#1 sun14php

sun14php
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 29 June 2006 - 07:10 AM

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


#2 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 29 June 2006 - 07:41 AM

I shall make a start for you...
Note: You will need to add validation and error checking, this serves as example only.

Adding:
<?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
?>

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

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

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

hth, Zac.



#3 sun14php

sun14php
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 29 June 2006 - 09:26 AM

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

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 29 June 2006 - 09:31 AM

[!--quoteo(post=389173:date=Jun 29 2006, 09:26 AM:name=sun14php)--][div class=\'quotetop\']QUOTE(sun14php @ Jun 29 2006, 09:26 AM) View Post[/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"
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 sun14php

sun14php
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 29 June 2006 - 10:23 AM

[!--quoteo(post=389174:date=Jun 29 2006, 04:31 AM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Jun 29 2006, 04:31 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$date=("dmy");

input name="date"
[/quote]


this code didn't help too.

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 29 June 2006 - 12:37 PM

[!--quoteo(post=389188:date=Jun 29 2006, 06:23 AM:name=sun14php)--][div class=\'quotetop\']QUOTE(sun14php @ Jun 29 2006, 06:23 AM) View Post[/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:
// 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";
}

good luck
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users