1

I have some dates stored in YYYY-MM-DD format in a MySQL database that I need to pass to JQuery for use in a JQuery UI Calendar. The problem I'm having is that `2014-01-12 in PHP is January 12 2014, but February 12, 2014 in Javascript.

This is because 0 = January in Javascript.

So how can I reliably pass a date to Javascript?

I've tried doing a simple strtotime "-month", but obviously that's not actually what I want -- I don't want exactly a month to be removed, I want the date to remain the same, but in a different format.

Thanks!

Update:

<script>
$(document).ready(function() {

<?php 
$startDate = date("Y,n,j", strtotime('-1 month'));
$endDate = date("Y,n,j", strtotime('+1 year -1 month')); 
?>

var startDate = new Date(<?php echo $startDate; ?>);
var endDate  = new Date(<?php echo $endDate; ?>);
8
  • 3
    The problem I'm having is that 2014-01-12 in PHP is January 12 2014, but February 12, 2014 in Javascript. -- Show us how you pass the dates. Commented Dec 27, 2013 at 20:46
  • Months are zero based in javascript, somewhere you're messing something up! Commented Dec 27, 2013 at 20:47
  • @adeneo Isn't that exactly what I say in my question? Commented Dec 27, 2013 at 20:49
  • Missing quotes, maybe? var startDate = new Date("<?php echo $startDate; ?>"); EDIT: ah no, never mind; I overlooked the comma's... Commented Dec 27, 2013 at 20:51
  • If you're using strtotime, why not just pass the unix timestamp, that would be the same in PHP, Javascript and anywhere else ? Commented Dec 27, 2013 at 20:52

2 Answers 2

2

In JavaScript, months are zero-based. It might be nonsense at first, but consider this:

var names = ["Jan","Feb","Mar"...];
var thisMonth = names[date.getMonth()];

Pretty cool! But yeah, it's a gotcha, and subtracting a month in PHP won't fix it (especially if you're working in January).

You will need to subtract one from the months in the JavaScript side. Try this:

alert(new Date(<?php echo date("Y,n-1,j"); ?>));
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Exactly what I needed. No messing around with Unix Timestamps either. Thanks.
1

Something like this, should do the job:

Database query:

SELECT UNIX_TIMESTAMP(date_field) AS mydate FROM table;

In Javascript:

var jsDate = new Date(<?php echo mydate; ?> * 1000);

4 Comments

I think nobody got the point...in this way we works with numbers, and got the right date...thanks a lot downvoter ;)
The rest of the project works with date. I think I'd rather stay with that than introducing new date formats, thanks.
Don't worry, it's always good to have a set of answers, instead of just one...more choices, one never knows
Not portable to some other DB engines, but still a very elegant solution. +1

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.