Insert Java Script Date in PHP Echo Statement

By twanstall ·
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:

<script language="JavaScript" type="text/javascript">

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????

<?php echo <script language="JavaScript" type="text/javascript">
</script> ?>

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

Thank You


This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Why not PHP?

by Toivo Talikka In reply to Insert Java Script Date i ...

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 The functions are in subtimedate.php.

Collapse -

you're 99% there

by mac1ne In reply to Insert Java Script Date i ...

you just need to tweak your example slightly...

echo "<script language='JavaScript' type='text/javascript'>

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.

Collapse -

Comment on monthname array

by hosamaly6 In reply to Insert Java Script Date i ...

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.

Related Discussions

Related Forums