Jump to content

PHP to JavaScript Equivalent


unemployment

Recommended Posts

Something like this? (I didn't verfiy this)

 

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

currentDate = year + "-" + month + "-" + day

if (minutes < 10){
minutes = "0" + minutes
}
thetime = hours + ":" + minutes + " "

if (hours > 11) {
ampm = "PM"
} else {
ampm = "AM"
}
fulldatetime = currentDate + " " + thetime + " " ampm

If you're using AJAX, then I'm assuming you're connecting to a PHP script. Why not just have PHP generate the date from there? Or if you want the date to be selectable / relative to the client-time, then you could pass in a unix timestamp (which is much easier to generate with JS) and again let PHP handle the formatting.

I agree.  I would always use PHP instead of javascript if possible.

both have there pros and cons

Care to elaborate?

well its a situational kind of thing...it depends what you need to be done to declare which language would be better suited for you...i wouldnt go as far as saying that I would always use PHP instead of js...

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.