View Full Version : Javascript changes date format?

03-19-2012, 06:38 PM

I have got this kind of array:

$days = array('Monday' => '2012-03-19',
'Tuesday' => '2012-03-20'

I'm iterating like this:

foreach($days as $key => $value){
echo '<a href="javascipt:confirmDelete('.$value.');">Delete</a>';


function confirmDelete(day)
if (confirm('Delete?')) {
window.location.href = 'processDay.php?action=delete&day=' + day ;

While i hover over the link with coursor, it show it good: javascript:confirmDelete(2012-03-19), but when I submit the delete confirmation, i get &day=1987. Where is the problem?

Old Pedant
03-19-2012, 10:29 PM
2012-03-19 is a subtraction expression.

2012 minus 3 is 2009.

2009 minus 19 is 1990.

So I don't know why you get 1987 instead of 1990.

Now try putting quotes around your date.

foreach($days as $key => $value){
echo '<a href="javascipt:confirmDelete(\''.$value.'\');">Delete</a>';

rnd me
03-20-2012, 02:12 AM
turn a php var into js using json_encode:

echo '<a href="javascipt:confirmDelete('. json_encode($value) .');">Delete</a>';

oh, and by the way, confirm() is assertive, not inquisitive, so re-word your question to reflect ok/cancel instead of yes/no for best UX:

if (confirm('Deleting...')) {