Question Locked

Insert Java Script Date in PHP Echo Statement

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi all,

i was hoping for some technical help using php and mysql.

I have a simple database connected (mysql) and website (php/html).

A form of this website involves me inputting a date field in it. i have created a date function using the following javascript code:

var todaysdate = new Date();
year = todaysdate.getFullYear();
month = todaysdate.getMonth();
date = todaysdate.getDate();
day = todaysdate.getDay();

var monthname = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
var dayname = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

The date is inserted into the webpage using the following code:

.

Basically i want to be able to insert this javascript date (by default) into the date field of my form, using the php echo method, if it is possible????

Example:

document.writeln(year+"-"+monthname[month]+"-"+date);
?>

Please help as this is doing my head in!!!!!

Thank You

tom

4 replies

by toivo talikka

Do you have a particular reason to use the date from javascript, like catering for different timezones of the users, instead of using the date from the server, provided by PHP functions?

I have some GPL code using dates on PHP forms, available from http://totaldata.biz/techtips/phpmysql.html. The functions are in subtimedate.php.

by mac1ne

you just need to tweak your example slightly...

echo "";

i have no idea if this will appear properly in this post though...if not just add double quotes to the start and end of your echo statement, and use single quotes inside the JavaScript part inside that echo.

by hosamaly69

I'm sorry that this reply is not really related to your problem, but it's just something I noticed in the code. As far as I understand, you're making the monthname array just to get 2-digit numbers for your months. If this is its only use, then I would suggest you replace it with the following:

if (month < 10) monthstr = "0" + month;
else monthstr = "" + month

You could also use a more compact form:

monthstr = (month < 10 ? "0" : "") + month;

This way you do not need the array.

This discussion is closed to new replies.