0

I've noticed that in JavaScript, when creating a Date, months are zero based.

So I input (from a mysql php call) a value like "2019, 1, 1". I get this value via:

date("Y, m, d",strtotime($mysqlcall['recorded']))

So input that value:

var foo = new Date(2019, 1, 1)

but it produces February 1st 2019. How can I fix that problem?

asked Dec 23, 2019 at 15:44
4
  • 2
    Subtract 1 from the month in JavaScript. Commented Dec 23, 2019 at 15:44
  • how can I do that? And is it possible to directly change the String in php? Maybe substract -1 from "m"? Commented Dec 23, 2019 at 15:47
  • he could but thats a silly solution Commented Dec 23, 2019 at 15:50
  • date('Y, m-1, d', strtotime($mysqlcall['recorded'])) Commented Dec 23, 2019 at 18:48

1 Answer 1

1

Use the DateTime class:

<?php
$date = new DateTime('2020-01-01 00:00:00'); // Happy new year!
$month = $date->format('m') - 1;
echo $date->format('Y-' . $month . '-d H:i:s');

https://www.php.net/manual/en/class.datetime

answered Dec 23, 2019 at 15:49
Sign up to request clarification or add additional context in comments.

1 Comment

glad i could help!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.