I need to calculate the difference between two dates in hours and in minutes which is what the code below does. The only issue I have is that the dates and the location (timezone) has to be input by the user. How do I achieve that? Can be a different language from PHP as I only need a flowchart and the pseudocode.


$departure = new DateTime('8pm March 27, 2016 America/New_York');

$arrival = new DateTime('7:20am March 28, 2016 Europe/London');

$flighttime = $departure->diff($arrival);

echo "$flighttime-> h hours $flight_time->i minutes";

Output: 7 hours 20 minutes

  • $\begingroup$ What's the problem exactly? That the '8pm March 27, 2016 America/New_York' string comes from user input? Are you asking how to validate it? $\endgroup$ – Karolis Juodelė Nov 22 '16 at 14:24
  • $\begingroup$ $departure = new DateTime('8pm March 27, 2016 America/New_York'); Inside of the bracket, I need that to be the user input, so the user inputs that information. $\endgroup$ – James Nov 22 '16 at 14:44
  • $\begingroup$ So you're asking how to take a string from a some textarea in some html and run your code on it? $\endgroup$ – Karolis Juodelė Nov 22 '16 at 14:59
  • $\begingroup$ Yes, I want the user to input the date and the location from which the timezone is taken and then run the code on it to calculate the difference between the two inputted dates. $\endgroup$ – James Nov 22 '16 at 15:05
  • 2
    $\begingroup$ You're asking for an algorithm but this is something for which you should use your programming environment's built-in date and time handling routines, rather than trying to roll your own code. This is the sort of thing where reinventing the wheel just causes mistakes from obscure cases. $\endgroup$ – David Richerby Nov 22 '16 at 16:25

TimeA -> ConvertToTimeZoneUtc -> NormalizeToGreenwichUtcTime -> ConvertToTicks Same for timeB ( ticksA - tickB) => timepsan. GetHours | GetMins

Is that ok?

| cite | improve this answer | |
  • 2
    $\begingroup$ Welcome to Computer Science! It seems like a good idea converting to one format and make calculations there but a textual description would be nice and please make sure that question is on-topic and not a duplicate before providing an answer. $\endgroup$ – Evil Nov 22 '16 at 19:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.