Skip to content

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

Open
belgattitude opened this issue Nov 14, 2015 · 1 comment
Open
Assignees

Comments

@belgattitude
Copy link
Owner

Hide the complexity of using dates when JavaVM and PHP timezones differs.

@belgattitude
Copy link
Owner Author

Before that tasks is finished it's interesting to have a look to the updated docs:

Working with dates

Dates are not (yet) automatically casted between Java and PHP. Keep in mind that internally the JVM
keeps tracks of milliseconds where PHP is limited to microseconds and that timezones might differs between
runtimes.

Ignoring deprecated constructors, the java.util.Date allows
creation of dates based on a timestamp expressed in milliseconds :

<?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
to parse the date string without the php conversion.

<?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
are not guaranteed to be the same (think of 2016-12-31 23:00:00 in London and Paris)

In most cases those differences can be easily fixed by ensuring both the JVM and PHP configurations
use the same timezone.

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);

@belgattitude belgattitude self-assigned this Jan 18, 2017
belgattitude added a commit that referenced this issue Apr 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant