Jump to content

Dates?


papaface

Recommended Posts

Hello,

I'm not very good with working with dates so I need help asap.

I have a form input text box that the user puts a date in. In this format 10/04/2007.

How can I put that into a database, and then retrieve the results and sort them by date?

 

regards

Link to comment
Share on other sites

<?php

$date = strtotime($_POST['date']);
echo $date;

$convert = date('d-m-Y',$date);

echo "<br><br>". $convert;

?><form action="test.php" method="post"><input name="date" type="text" id="date" /></form>

 

when i use that, and put in the date 05/04/2007 (april the 5th) it changes it so it comes out 04-05-2007 (4th of april)

Link to comment
Share on other sites

I have a site that uses dates - and to insert I use:

 

		
$date = $db->escape($_POST['date']);

$db->query("INSERT INTO table SET date = '$date'");

 

The to display the date on a page:

 

echo date("l - F j, Y",strtotime($date));

 

Which displays dates as Tuesday - April 10, 2007

 

 

Hope that helps.

Link to comment
Share on other sites

I use this code to put it into the db:

		$date = strtotime($_POST['date']);
	$convert = date('Y-m-d',$date);


	$_insert = mysql_query("insert into `requests` (name,address,postcode,telephone,email,inspectaddress,stage,area,date,status) values ('".$_POST['name']."','".$_POST['custaddress']."','".$_POST['postcode']."','".$_POST['telephone']."','".$_POST['email']."','".$_POST['inspectaddress']."','".$_POST['stage']."','".$_POST['area']."','{$convert}','unassigned')");

 

It goes in like this:

2007-05-04

I then try retrieve the date and it comes out like this 2007-05-04.

How can I change it, so it goes day (05) month (04) and then year?

I tried:

			while	(list($request_id,$name,$address,$postcode,$telephone,$email,$inspectaddress,$stage,$area,$date) = mysql_fetch_array($_getunassigned))

		{

		$date = gmdate('d-m-Y',$date);

 

But it doesnt work

Link to comment
Share on other sites

Should be stored as Y-m-d . So 2007-04-09. When it's being entered into the form, are you entering as 09-04-2007 - then converting to 2007-04-09. If it's just  you using the form, you could skip the conversion and enter it as 2007-04-09.

Link to comment
Share on other sites

Actually looking back - if you enter in the format of 31-4-07 (I'm using 31 so we know its a day not a month) and convert to date(Y-m-d) then when you call it up you can use:

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

 

Try that out.

Link to comment
Share on other sites

How it was put into the database is fine... change how you call it up:

 

while	(list($request_id,$name,$address,$postcode,$telephone,$email,$inspectaddress,$stage,$area,$date) = mysql_fetch_array($_getunassigned))

		{

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

 

When you get that date of 1970-01-01 you know theres an error - thats the date MySQL starts counting from.

Link to comment
Share on other sites

$date must be a unix time stamp to be manipulated. It seems as though you ignored what JSHINER wrote completely...

No i didnt. It is converted:

$date = strtotime($_POST['date']);

$convert = date('Y-m-d',$date);

But now it doesnt work.

		$date = strtotime($_POST['date']);
	$convert = date('Y-m-d',$date);


	$_insert = mysql_query("insert into `requests` (name,address,postcode,telephone,email,inspectaddress,stage,area,date,status) values ('".$_POST['name']."','".$_POST['custaddress']."','".$_POST['postcode']."','".$_POST['telephone']."','".$_POST['email']."','".$_POST['inspectaddress']."','".$_POST['stage']."','".$_POST['area']."','{$convert}','unassigned')");

 

That is my code.

 

It is entered into the input box like this: 31/04/2007 I want it to come out like that aswell.

I'm sorry for all this confusion, but I just hate dates.

Link to comment
Share on other sites

Ok the way it goes in is fine.

 

Now let's pull it from the database using the follwoing code:

 

while	(list($request_id,$name,$address,$postcode,$telephone,$email,$inspectaddress,$stage,$area,$date) = mysql_fetch_array($_getunassigned))

		{

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

 

That code will display 2007-04-31 (the way it is stored) as 31-04-2007. date("d-m-Y") is day-month-year.

 

Let me know if that works for you. They key to dates is getting them into the database correctly. Once they're in there correctly, you can manipulate them however you would like.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.