3

I have a php date and wish to echo it out in a javascript alert box:-

$day=15;
$month=8;
$year=2012;

$date_display = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
echo $date_display; // 2012-08-15

Then,

<a href="#" onclick="give_date(<?=$date_display;?>)"><?=$day;?></a>

The javascript function:

<script>
function give_date(value){
alert (value);  
}
</script>

Interestingly, the alert box give me "1989", which equals to 2012 minus 8 minus 15!! what shall I do!!

9
  • Since you use date it shouldn't make much difference, but in general, you're making a mistake here by confusing between the client-side and the server-side. The value of $date_display is calculated on the server-side while onclick is called on the client side. As for your problem, try casting to String. Commented Aug 2, 2012 at 4:55
  • @alfasin: nope, it's not true Commented Aug 2, 2012 at 4:59
  • @alfasin: the thing that "The value of $date_display is calculated on the server-side". This thing would confuse OP even more, since it's not clear what "calculated" here means: either initialization with '2012-08-15' string value or math evaluation. And second - "As for your problem, try casting to String" --- to cast to string what? In php it is a string - makes no sense, in js - it's too late, so makes no sense as well Commented Aug 2, 2012 at 5:02
  • @zerkms when I said "cast to string" I meant adding quotes - my bad. But, All the first part of my comment is valid. Many programmers confuse server-side with client-side. The date is calculated on the server-side, so if the server is in US and the customer is in Europe, the browser might show that date of yesterday (from the client perspective). Confusing ? too bad, programming isn't always easy :) Commented Aug 2, 2012 at 5:07
  • By the way, it is better to use <?php echo $date_display; ?> rather than <?=$date_display;?>. And don't use mktime, use time. Commented Aug 2, 2012 at 6:24

3 Answers 3

3

Now you get: <a href="#" onclick="give_date(2012-08-15)">15</a>, so it calculates it in browser.

the solution is simple - add quotes:

<a href="#" onclick="give_date('<?=$date_display;?>')"><?=$day;?></a>

Then you get: <a href="#" onclick="give_date('2012-08-15')">15</a>

Sign up to request clarification or add additional context in comments.

Comments

1

You are making the client-side code show an alert with the date of the server when the page was loaded.

If you want to show the user the current time, use this:

HTML:

<a href="#" id="date">Please enable JavaScript.</a>

JavaScript:

<script type="text/javascript">
function show_date() {
    alert(new Date());  
}
window.onload = function() {
    document.getElementById('date').innerHTML = new Date().getDay();
};
document.getElementById('date').onclick = show_date;
</script>

Comments

0

You can use jQuery if you are up for it

<a href="#" id="<?php echo $date_display; ?>">Click to get date popup</a>

$(function(){

 $('a').click(function(){

       var dt = $(this).attr('id');
       alert(dt);
 });

});

4 Comments

2012-08-05 is not a good candidate to be used as an id. I'd suggest to use data-date="..." instead
@zerkms A little curious here, why not?
Hmm...but it serves what the OP wants right? I guess it can be overlooked
id should begin with letter. It's how standard defines requirement to id. You may do whatever you want, but the standard is a standard. And it is a good idea to follow it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.