-
Notifications
You must be signed in to change notification settings - Fork 15
Provide a way to easily work with dates between Java and PHP #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Before that tasks is finished it's interesting to have a look to the updated docs: Working with datesDates are not (yet) automatically casted between Java and PHP. Keep in mind that internally the JVM Ignoring deprecated constructors, the java.util.Date allows <?php
// $ba = new BridgeAdapter(...);
$phpDate = \DateTime::createFromFormat('Y-m-d', '2016-12-21');
$milli = $phpDate->format('U') * 1000; // Internally the JVM handles milliseconds
// In order to create a new Java date,
// php dates must be converted accordingly.
// The 'U' allows formatting the date as
// microseconds since epoch time, just multiply
// by 1000 to get milliseconds.
// Alternatively you can use
// $milli = strtotime('2016-12-21') * 1000;
$javaDate = $ba->java('java.util.Date', $milli);
$simpleDateFormat= $ba->java("java.text.SimpleDateFormat", 'yyyy-MM-dd');
echo $simpleDateFormat->format($javaDate);
// Will print: "2016-12-21" Alternatively you can use the java.text.SimpleDateFormatter object <?php
// $ba = new BridgeAdapter(...);
$date = '2016-12-21';
$simpleDateFormat = $ba->java("java.text.SimpleDateFormat", 'yyyy-MM-dd');
$javaDate = $simpleDateFormat->parse($date); // This is a Java date
echo $simpleDateFormat->format($javaDate);
// Will print: "2016-12-21" Please be aware that timezones might differ from PHP and the JVM. In that case, dates between PHP and Java In most cases those differences can be easily fixed by ensuring both the JVM and PHP configurations Another option is to pass the current timezone in the formatter : <?php
$pattern = "yyyy-MM-dd HH:mm";
$formatter = $ba->java("java.text.SimpleDateFormat", $pattern);
$tz = $ba->javaClass('java.util.TimeZone')->getTimezone("Europe/Paris");
$formatter->setTimeZone($tz); |
Hide the complexity of using dates when JavaVM and PHP timezones differs.
The text was updated successfully, but these errors were encountered: