Update Date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TNIDBMNG
Forum Newbie
Posts: 21
Joined: Wed Aug 18, 2004 12:22 pm

Update Date

Post by TNIDBMNG »

I have a text box which stores a date from a table I have in MySQL. I want the user to be able to change the date if they would like which works fine, the problem I'm having is I have to display the date in the text box as dd-mmm-yyyy. If the user updates another field and leaves the date as that format MySQL doesn't recognize the date so it changes the date to NULL. I tried the Date() function but it changes the date to 1969-12-31. How can I change the format of the text box to be MySQL's format before the database is updated? Here is the code I currently have.

Code: Select all

<?php
$p_priority=date("Y-m-d",$_POST[$priorityfield]);
$sql="UPDATE tblITPriorities SET dueDate='".$duedate."', priority='".$p_priority."', completed='".$p_completed."', dateCompleted='".$datecompleted."' WHERE ID=".$p_id.";";
mysql_query($sql,$cnx);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's easier to actually have 3 inputs, one for month, day, and year. Each can be a textbox, but it's often easier to have drop-downs for at least month and day. Although the validation routine remains the same for all.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

For each timestamp that will be outputted, i add this to my SELECT clause
DATE_FORMAT(timestamp, '%Y-%m-%d %H:%i) AS timestamp

This way the user is presented a "nice" date ;)


But if you want to use a unix time to put in a mysql timestamp use:

FROM_UNIXTIME($date)
TNIDBMNG
Forum Newbie
Posts: 21
Joined: Wed Aug 18, 2004 12:22 pm

Post by TNIDBMNG »

Thanks Tim but I'm not outputting the data I'm trying to update the data in the database. I currently use date_format for outputting but does it work the same way when updating the database?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What is the MySQL column type holding the date ? datetime, timestamp etc.. ?
TNIDBMNG
Forum Newbie
Posts: 21
Joined: Wed Aug 18, 2004 12:22 pm

Post by TNIDBMNG »

datetime
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

A datetime is in YYYY-MM-DD HH:MM:SS format so you need to use:
date('Y-m-d h:i:s')
Post Reply