Jump to content

Dates?


papaface

Recommended Posts

<?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
https://forums.phpfreaks.com/topic/46467-dates/#findComment-226083
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
https://forums.phpfreaks.com/topic/46467-dates/#findComment-226085
Share on other sites

Also - heres a list of the formats for dates - http://us.php.net/manual/en/function.date.php so you can output it however you would like.

 

Just remember, a date field in an a MySQL database is 2007-04-10 - so either have them entered like that, or converted to that before they are inserted to the database.

Link to comment
https://forums.phpfreaks.com/topic/46467-dates/#findComment-226097
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
https://forums.phpfreaks.com/topic/46467-dates/#findComment-226137
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
https://forums.phpfreaks.com/topic/46467-dates/#findComment-226188
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
https://forums.phpfreaks.com/topic/46467-dates/#findComment-226192
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
https://forums.phpfreaks.com/topic/46467-dates/#findComment-226196
Share on other sites

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.