Android Question php strtotime equivalent in B4A

nwhitfield

Active Member
Licensed User
Longtime User
There are DateTime Parse functions; you will need to set the format string, which the Java docs suggest is ""EEE, d MMM yyyy HH:mm:ss Z"" to match the example you have there.
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
There are DateTime Parse functions; you will need to set the format string, which the Java docs suggest is ""EEE, d MMM yyyy HH:mm:ss Z"" to match the example you have there.

Thanks for fast replay
the problem that i have more than one format (php never had a problem with that)
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
I tried parse the date but i get unparsable date exception
B4X:
    DateTime.DateFormat = "EEE, d MMM yyyy HH:mm:ss Z"
    Log(DateTime.("Mon, 16 Oct 2017 06:53:59 +0000",""))

any ideas ?
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
B4X:
  DateTime.DateFormat = "EEE, d MMM yyyy HH:mm:ss Z"
   Log (DateTime.DateParse("Mon, 16 Oct 2017 06:53:59 +0000"))

Result is 1508136839000; drop the last three digits, and paste into EpochConverter.com to confirm it works
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
B4X:
  DateTime.DateFormat = "EEE, d MMM yyyy HH:mm:ss Z"
   Log (DateTime.DateParse("Mon, 16 Oct 2017 06:53:59 +0000"))

Result is 1508136839000; drop the last three digits, and paste into EpochConverter.com to confirm it works
thanks buddy
is it possible to do that to any time format?
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
You'd need to test against different formats to see which was the right one to use. What's the context of the data you have here? Is it something like an email message header, for example? Or data obtained from the internet? Do you control the source? Can your app always be assumed to have connectivity?

If the data is coming from a database, for example, you can get the SQL query to calculate a timestamp (syntax may differ, this works for MySQL)

B4X:
SELECT appointmentDate, UNIX_TIMESTAMP(appointmentDate) AS apptTS FROM appointments

If the data is in a well-defined header (for example, the Date: header of an email message), you should be able to narrow it down to just a few formats to try - check the relevant RFCs to see what's allowable.

If you know you'll have connectivity, and latency isn't an issue, and you have a server with PHP, you could also let it do the work, by POSTing to a script like

B4X:
date_default_timezone_set('UTC') ;
$unixtime = strtotime($_REQUEST['TIME']) ;
$results = array('request' => $_REQUEST['TIME'], 'timestamp' => $unixtime );
print json_encode($results);

and then handling the JSON result. You could, perhaps, combine a selection - check in your code for the most common formats that you come across in your data, and if you find a date string that you can't handle, throw it at the webservice, which can also log the request, for you to work out the format and add it to the next version of the app.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top