1

I have the following PHP code to convert an arbitrary String into a DateTime parameter, and then into an interval between the two dates. However it isn't working. What's the problem?

//$dateOfBoat = '2017 29 Dec 05:13:37 AM '
$date = date_create_from_format('Y d M h:i:s A', $dateOfBoat);
$dateTime = date_format($date, 'Y-m-d H:i:s');

//Time now - THIS WORKS FINE
$date_now = time();

$boatFinishDateTime = strtotime($dateTime);

//Find interval between two dates
$interval = $date_now-$boatFinishDateTime;

Thanks.

Seb O
  • 43
  • 6

1 Answers1

2

Thanks for posting the rest of the code, please refer to this fiddle

http://sandbox.onlinephpfunctions.com/code/78d2b6613dbadff5a79ab7ebed3cba304f1188fe

Here is the code

$DateTime = date_create_from_format('d M, h:i:s A ', $dateOfBoat);
$boatFinished = $DateTime->getTimestamp();
print_r("boatFinished: $boatFinished\n");

$now = time();
print_r("now: $now\n");

//Find interval between two dates
$interval = time()-$boatFinished;
print_r("Interval: $interval");

And the Output

boatFinished: 1514553217
now: 1509506464
Interval: -5046753

We can simplify things and take advantage of our DateTime object. Because of that there is no need to output the date from the DateTime as a formatted string, and then convert that to a timestamp. We can instead get a timestamp directly from the dateTime object using it's getTimestamp() method.

Once we have that as a unix timestamp, the rest is pretty trivial ( which you already had ). Get the current time time() which is a timestamp. Then we just subtract them and we have the number of seconds between the two events.

You were very close.

UPDATE

Please refer to this second fiddle

http://sandbox.onlinephpfunctions.com/code/7d13d0319e266906df22624e701586914627f9dd

The first one is what I call a naive implementation, which just means that it does not consider enough edge cases. For this I would classify those as consiting of input other then we expect for the $dateOfBoat variable. The main 2 I can think of are

  1. An empty input
  2. Input with an invalid format, or the completely wrong value

For both of these we can check if the DateTime object was created using a simple Boolean comparison. After we try to create the DateTime, but before we use it we just need to add a simple condition, like this:

$DateTime = date_create_from_format('d M, h:i:s A ', $dateOfBoat);

if( !$DateTime )  //check that our DateTime object was properly created
    throw new Exception("Invalid dateOfBoat input[{.$dateOfBoat}]");

$boatFinished = $DateTime->getTimestamp();

Here I am just throwing an Exception, It's up to you to decide how you want to handle this in your application. But raising an exception and handling it where this was called from, is certainly an option.

Cheers!

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • This doesn't answer the question. If there's code in your external link, please copy the main parts into your answer – Phil Nov 01 '17 at 03:06
  • Also, `strtotime` does **not** return a date-formatted string. It returns an integer timestamp (seconds since unix epoch) – Phil Nov 01 '17 at 03:09
  • Um, 1. I'm not done, 2. I was looking at the wrong output line for strtotime, that's actually `date_format`. I have my way of doing things, I need to work my way through it, so yea.. be patient. – ArtisticPhoenix Nov 01 '17 at 03:10
  • +1 @ArtisticPhenoix. Phil, people on here give up there time for the good of others. Don't expect them to always work how you desire. – Seb O Nov 01 '17 at 03:17
  • @SebO I have no idea what you're talking about. There's a comprehensive set of guidelines for writing answers here ~ https://stackoverflow.com/help/how-to-answer. Pay specific attention to the *"Provide context for links"* section – Phil Nov 01 '17 at 03:21
  • Thank you @ArtisticPhoenix! – Seb O Nov 01 '17 at 03:23
  • @ArtisticPhoenix, I'm getting the error, 'Fatal error: Call to a member function getTimestamp() on boolean in ------.php on line -- '. Any ideas on how to fix? – Seb O Nov 01 '17 at 03:27
  • Sorry guys if I work on the page. I know sometimes this can cause miss-understandings, But as I am sitting in bed with my laptop ( Lenovo Y700, 15in) on my lap, watching TV, I find it hard to have multiple pages open without my extra screen ( 24inch ). – ArtisticPhoenix Nov 01 '17 at 03:27
  • @SebO - First thought is that your dateTime object was not created, this can happen if you have an invalid date string, or it doesn't match the format given in `date_create_from_format`. You could add some error checking between these to catch that. I will update my answer to throw an exception if that happens. – ArtisticPhoenix Nov 01 '17 at 03:29
  • @ArtisticPhoenix Still no luck unfortunately. What do you mean specifically? I've copied your code and run it, to find that the DateTime is nil. Strange.. – Seb O Nov 01 '17 at 03:34
  • @ArtisticPhoenix Thanks for the comprehensive update! The is an exception being called. The input is, "29 Dec, 05:13:37 AM ' - what could be the problem? – Seb O Nov 01 '17 at 03:52
  • @SebO - are you sure, the input value should be contained between the `[]` in the new Exception I added. For example `Fatal error: Uncaught Exception: Invalid dateOfBoat input[foo bar]` The `[foo bar]` bit, if it is just the brackets `Exception: Invalid dateOfBoat input[]` then your input is empty. – ArtisticPhoenix Nov 01 '17 at 03:58