Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php date convert to javascript date

Tags:

javascript

php

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

like image 971
Ham Avatar asked Jun 23 '26 18:06

Ham


1 Answers

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>

like image 95
Danil Speransky Avatar answered Jun 26 '26 07:06

Danil Speransky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!