Richard
Richard

Reputation: 2815

strtotime: Check for time

I have a voucher system, where I can enter a date or a date with a time. There should be all valid formats allowed, for example this ones:

This I no problem, if I have the start date for a voucher, but it is a problem if I had the end date. I use strtotime to convert the user input to a UNIX timestamp.

If you just enter the date as end date, it should be the end of the day used. Like this:

01.01.2016 -> 01.01.2016 23:59:59 -> 1451689199

But - and this is my problem - strtotime returns the same timestamp of course for 01.01.2016 and 01.01.2016 00:00:00. And when the user enters a time, this time should be used of course.

01.01.2016
1451602800
-> should go to 1451689199

01.01.2016 00:00:00
1451602800
-> is correct

01.01.2016 23:59:59
1451689199
-> is correct

I need a possibility to check if the string - that is converted by strtotime - has a time explicit in it. I searched for a function for this without success, even the DateTime class has no method for this (hasTime() or something like this).

As I said before, all date/time formats strtotime supports should be supported by this function also.

This question is NOT a duplicate! I want to check if there is any time specified explicit. The question which should be the duplicate is a basic question for PHP beginners and has exactly nothing to do with this problem!

Upvotes: 5

Views: 2758

Answers (4)

eselskas
eselskas

Reputation: 817

By default, all timestamps include time, even if it's not set, it will be 00:00:00.

So in this case, you could do something like this:

<?php 

$notime = 1420070400;
$withtime = 1420110000;


$time = date("H:i:s", $withtime);

if($time == "00:00:00") {
    echo "time not set";
} else {
    echo "time set";
}

?>

Upvotes: 0

RepeatQuotations
RepeatQuotations

Reputation: 724

Judging by your provided timestamps, the unix epoch conversions taken as input are all in GMT time. If this is the case you could try a basic check of explicitly set time by using the gmdate function, making an assumption that if they have values, it is a user modified timestamp.

If not, the timestamp can be modified and the 23 hours, 59 minutes, 59 seconds balance can be added to the date.

<?php
  $unixstamp = '1451520000'; // Thu, 31 Dec 2015 00:00:00 GMT
  $day = gmdate( 'Y-m-d', $unixstamp ); // 2015-12-31
  $hour = (int)gmdate( 'H', $unixstamp); // 0
  $minute = (int)gmdate( 'i', $unixstamp ); // 0 

  //check if hours or minutes have been set explicitly 
 if( $hour > 0 || $minute > 0 ){
   //User Modified Time
   echo gmdate("c",$unixstamp);

 } else {
   //here add the 23 hours and 59 minutes on and return as date object
   echo gmdate("c", strtotime('+23 hours + 59 minutes + 59 seconds', $unixstamp));  

   //here add the 23 hours and 59 minutes on and return as unix epoch timestamp
   $endofday = gmdate("U", strtotime('+23 hours + 59 minutes + 59 seconds', $unixstamp));   
 }
?>

Timezones are definitely a consideration but the above code will check a GMT unix timestamp for explicitly set time.

Upvotes: 0

Toto
Toto

Reputation: 91488

I'd test the length of the string before calling strtotime and add 23:59:59:

$dates = array('01.01.2016','01.01.2016 23:59:59','01.01.2016 10:25:30');
foreach($dates as $date) {
    echo "date=$date\n";
    if (strlen($date) > 10) {
        echo strtotime($date),"\n";
    } else {
        echo strtotime($date . ' 23:59:59'),"\n";
    }
}

Output:

date=01.01.2016
1451689199
date=01.01.2016 23:59:59
1451689199
date=01.01.2016 10:25:30
1451640330

Upvotes: 1

birdspider
birdspider

Reputation: 3074

given an input of '2015031' or '2015031 10:39', you can basicly try the first one, if it returns false try the other.

no guarantees on robustness though

php > var_dump(DateTime::createFromFormat('Ymd H:s','2015031'));
 bool(false)
php > var_dump(DateTime::createFromFormat('Ymd H:s','2015031 10:29'));
 object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-03-01 10:00:29.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}

Obviously changing the format to your needs and extracting the TS with ->getTimestamp.

Upvotes: 0

Related Questions