-4

This is an example code. In my .html file, I am using following HTML + PHP-code:

Today is: <? print(Date("l F d, Y")); ?>.

I would expect to see:

Today is: monday October 4, 2010.

The only thing I see is:

Today is:.

(mind the dot at the end).

This means the PHP-code is not processed!

PHP-code in a .php file works fine.

I tried to add AddType application/x-httpd-php .htm .html and addtype application/x-httpd-php .php .htm .inc .html to the .htaccess file, neither of which helped.

Is there something wrong with my hosting-provider? (on Apache server) Or am making some mistake?
Please help, as I am puzzling for many days now.

Rakesh
  • 9
  • 7

3 Answers3

0

Save your file .html file with extension .php

For example if your file name is sample.html rename it to sample.php

And on sample.php file write the following code :

<?php

  echo "Today is: ".date("l F d, Y");

?>

Output : Today is: Tuesday December 30, 2014

See here DEMO

Edit :- If you don't want to use PHP then you can display current date using Javascript. See HERE for javascript solution

Community
  • 1
  • 1
Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79
0

Since you need to Display data ONLY using html page

So only way is using javascript

<span id="time"></span>

var today = new Date();
 document.getElementById('time').innerHTML=today;

JsFiddle

sanoj lawrence
  • 951
  • 5
  • 29
  • 69
  • This isn't quite right. HTML still comes from the server, and is processed by Apache before sending it to the client. You can configure Apache to send the `.html` document via the PHP parser prior to being sent to the client if you wish ([related question](https://stackoverflow.com/questions/6295141/server-not-parsing-html-as-php)). – halfer Dec 30 '14 at 12:18
  • @halfer thats right but `html` can run in localhost without server but `php` can't it need a server to run – sanoj lawrence Dec 30 '14 at 12:23
  • Sure, but the OP mentions a "hosting provider" and "Apache", so we can assume they have those. – halfer Dec 30 '14 at 12:24
  • @halfer thats right but i just mention that `php` code will not run in `.html` files as you commented then let me remove that line :) – sanoj lawrence Dec 30 '14 at 12:26
  • PHP won't run in HTML files _by default_. But it can be configured to do so, and that appears to be what the OP wants. See the `AddType` Apache directives in the question. – halfer Dec 30 '14 at 12:27
  • @halfer by configured we can make php run in `.html` ?? then i have to learn about that, thanks for teaching – sanoj lawrence Dec 30 '14 at 12:30
  • than what would be wrong after configured still no output – sanoj lawrence Dec 30 '14 at 12:32
  • Possibly the `.htaccess` file is not being read by Apache. Also there are different ways of running PHP, and perhaps it needs a different directive to enable the PHP engine for this file extension. Difficult to say without being able to try things, to be honest. – halfer Dec 30 '14 at 12:38
  • to be honest i don't have any knowledge about `.htaccess` where can i learn about that online – sanoj lawrence Dec 30 '14 at 12:41
  • @sanoj: if u dont hav any knowledge on htaccess, just keep calm.dont give wrong answers – Rakesh Dec 31 '14 at 04:46
  • @halfer: thanx a lot halfer for ur valuable answer:) – Rakesh Dec 31 '14 at 04:47
0

Instead of using php use javascript like this,

<button type="button"
   onclick="document.getElementById('demo').innerHTML = Date()">
   Click me to display Date</button>
<p id="demo"></p>
SMshrimant
  • 638
  • 9
  • 15