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!!
dateit 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_displayis calculated on the server-side whileonclickis called on the client side. As for your problem, try casting toString.'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<?php echo $date_display; ?>rather than<?=$date_display;?>. And don't usemktime, usetime.