Jump to content

[SOLVED] need help with date formating


grimmier

Recommended Posts

I have a web form set up to allow entries into a MySQL database,  my problem is that i need to be able to convert the date from mm-dd-yyyy format to yyyy-mm-dd format before it gets entered into the database, or it won't go.

 

I have tried to manipulate the string before entering it, but i may have made some errors. any help is greatly appreciated.

 

PS. the user enters the date so i need a way to fail-safe against m-d-yyyy formating as well, due to human nature to omitt the leading Zero's

Link to comment
https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/
Share on other sites

is the user using an arbitrary date, or can you just capture the current date at input time?

 

You can use either pulldowns or a js applet to make them pick a real date instead of typing in a date... much nicer IMHO

 

I actually need 2 dates  one for date received, and one for date cleared.  the date cleared is auto populated with the current date, which is nice. Its the date received that i worry about.

here is the post code i currently have.  it works but only if the date is in right format, otherwise the date fields are filled with 0's.

 

<?php
$con = mysql_connect("localhost","root","somepassword");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("2007_Inventory", $con);

$sql="INSERT INTO Inventory (`Maven ID`, `Owner ID`, `Date Received`, `Date Cleared`, `Status`, `Equipment Type`, `Make`, `Model`, `Serial Number`, `Processor`, `Speed`, `HD`, `RAM`, `Optical Drive`, `Tag #`, `Comments`, `Tested By`)
VALUES
('$_POST[mavenID]','$_POST[ownerID]','$_POST[date_rec]','$_POST[date_clear]','$_POST[status]','$_POST[type]','$_POST[make]','$_POST[model]','$_POST[serial]','$_POST[processor]','$_POST[speed]','$_POST[hd]','$_POST[ram]','$_POST[cd]','$_POST[tag]','$_POST[comments]','$_POST[tester]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>

 

if would be nice if i could change the format on behind the scenes before posting the data, and account for various formats.  or i might just do as was already recommended and just use a date picker, although this will slow down entry some.

 

PS. this is all set up on an intranet.

$time = $_POST['date_clear'];

$time = strtotime($time);

$time = date("Y-m-d", $time);

 

Then insert $time in your query instead of the POST var.

 

I tried this, with the date entered being 01-05-2007 and the output was 1969-12-31

 

 

your other option would be mktime:

 

$inputTime=explode($_POST['date_clear'], "-");

$time=date("Y-m-d", mktime($inputTime[2], $inputTime[0], $inputTime[1]));

 

this will always output the current date.  i need to be able to enter a date, thats not the current one

I just tested this code, and it works:

 

Can't believe I posted explode with the delimiter as the second argument though... I must be losing my mind.

 

<?php
$string="1-5-07";
$inputTime=explode("-", $string);
$time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2]));
echo $time;
?>

I took it a step further, incase anyone else has this problem.

 

I put in an if statement to check for users using a slash instead of a dash.

 

$string=$_POST['date_rec'];
$dash  = '-';
$pos      = strripos($string, $dash);

if ($pos === false) {
$inputTime=explode("/", $string);
$time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2]));
} else {
$inputTime=explode("-", $string);
$time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2]));
}

Archived

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

×
×
  • Create New...

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.