Adding days to $Date in PHP
All you have to do is use days instead of day like this: And it outputs correctly:
All you have to do is use days instead of day like this: And it outputs correctly:
tl;dr How to convert java.util.Date to java.sql.Date? Don’t. Both Date classes are outmoded. Sun, Oracle, and the JCP community gave up on those legacy date-time classes years ago with the unanimous adoption of JSR 310 defining the java.time classes. Use java.time classes instead of legacy java.util.Date & java.sql.Date with JDBC 4.2 or later. Convert to/from java.time if inter-operating with code not yet updated to java.time. Legacy Modern Conversion java.util.Date java.time.Instant java.util.Date.toInstant()java.util.Date.from( … Read more
You were close. You just need to use the duration.asHours() method (see the docs).
Not sure if this meets with the criteria of not setting up a new Calendar? (Why the opposition to doing so?)
date() itself is only for formatting, but it accepts a second parameter. To keep it simple I just subtract 24 hours from the unix timestamp. A modern oop-approach is using DateTime Or in your case (more readable/obvious) (Because DateInterval is negative here, we must add() it here) See also: DateTime::sub() and DateInterval
The recommended way to do this: Nowadays, you should really be using DateTime objects for any date/time math. This requires you to have a PHP version >= 5.2. As shown in Glavić’s answer, you can use the following: The ! formatting character is used to reset everything to the Unix epoch. The m format character is the numeric representation of a month, … Read more
Because you are passing a string as the second argument to the date function, which should be an integer. string date ( string $format [, int $timestamp = time() ] ) Try strtotime which will Parse about any English textual datetime description into a Unix timestamp (integer):
You don’t have a Date, you have a String representation of a date. You should convert the String into a Date and then obtain the milliseconds. To convert a String into a Date and vice versa you should use SimpleDateFormat class. Here’s an example of what you want/need to do (assuming time zone is not involved here): Still, be careful because in Java the milliseconds obtained are the … Read more
Marginally better…