View Full Version : Date function conversion from VBScript

07-26-2007, 01:05 PM
I'm making the transition from ASP to PHP and am trying to get my head around the changes in syntax. I'm drawing a blank on converting this bit of server side VBScript (I'm now wishing I'd learned ASP/Javascript rather than ASP/VBScript, as much more of what I've done would be more easily portable):

function ddmmyyyy(varDate)
ddmmyyyy = Day(DateValue(varDate)) & "/" & Month(DateValue(varDate)) & "/" & Year(DateValue(varDate))
end function

function mmddyyyy(varDate)
mmddyyyy = Month(DateValue(varDate)) & "/" & Day(DateValue(varDate)) & "/" & Year(DateValue(varDate))
end function

function yyyymmdd(varDate)
yyyymmdd = Year(DateValue(varDate)) & "/" & Month(DateValue(varDate)) & "/" & Day(DateValue(varDate))
end function


I use it as a standalone function that allows me to pass the date entered in a form in dd/mm/yyyy format to an Access database in mm/dd/yyyy format (I've had problems in the past with Access getting confused between British and American date formats, and I've found this to be a reliable method of making sure that months and days don't flip).

I wanted to use something similar with PHP/MySQL so that the date passed from a form to the database is yyyy-mm-dd format, but I'm unfamiliar with MySQL datatypes and functions and PHP. Could anyone give me a hint?

07-26-2007, 02:58 PM
How are you reading the date from your form?

If it's by using three input fields for year,month and day , you can easily append the result of the submited form in PHP as

$date = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];

where year,month and day are the names of the input fields, and should have the proper values in it.

07-27-2007, 03:29 PM
Hadn't thought of that - it's currently as a single field - but it's worth my having a look at that, thanks. The nice thing about my little VBScript is that it's portable - you can can feed any date to it, and it'll return the formatted version. So (forgive me for the VBScript in the PHP forum) I can use it to format text on a page

<%= ddmmyyyy(rsContent.Fields.Item("DateStart").Value%>
displays DateStart in dd/mm/yyyy

<input name="DateStart" type="text" id="DateStart" title="Enter the start date" value="<%= ddmmyyyy(rsCourse.Fields.Item("DateStart").Value) %>" size="12" maxlength="12" />
puts the DateStart value in ddmmyyyy in a field for an update form in my CMS, and

MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 135, 1, -1, MM_IIF(mmddyyyy(Request.Form("DateStart")), mmddyyyy(Request.Form("DateStart")), null)) ' adDBTimeStamp

Inserts the date into the database in mm/dd/yyyy format (using the Dreamweaver update server behaviour). It's just nice to be able to use the same function each time.