You need a function to determine whether the return time is valid.

Source: Internet
Author: User
You need to write a function isValidDate ($ date) with the following conditions: {code ...} when I wrote the second condition, I found that the time covered by the condition seemed to be a bit problematic. I 'd like to see your opinions.
  1. You need to write a function isValidDate ($ date) with the following conditions:

Function isValidDate ($ date) {// 1. $ date is the week + time () before on the day before $ date = true // 2. $ date: + time () next week. it must be = true after six o'clock P.M. this Thursday. // 3. otherwise, false is returned. (Note: One week starts from Monday) $ orderTime = strtotime ($ date); $ now = time (); if (date ('W', $ orderTime) = date ('W', $ now) & (strtotime ($ date, $ now)-$ now)> 86400/4) // book on the previous day, closing reservation {return true;} if (date ('W', $ orderTime) = date ('W', $ now) + 1 & $ now> strtotime ('Saturday six o'clock P.M.-2 Day', $ now) // book for the second week, Thursday and later {return true;} return false ;}

When I wrote the second condition, I found that the time covered by the condition seemed to be a bit problematic. I 'd like to see your opinions.

Reply content:
  1. You need to write a function isValidDate ($ date) with the following conditions:

Function isValidDate ($ date) {// 1. $ date is the week + time () before on the day before $ date = true // 2. $ date: + time () next week. it must be = true after six o'clock P.M. this Thursday. // 3. otherwise, false is returned. (Note: One week starts from Monday) $ orderTime = strtotime ($ date); $ now = time (); if (date ('W', $ orderTime) = date ('W', $ now) & (strtotime ($ date, $ now)-$ now)> 86400/4) // book on the previous day, closing reservation {return true;} if (date ('W', $ orderTime) = date ('W', $ now) + 1 & $ now> strtotime ('Saturday six o'clock P.M.-2 Day', $ now) // book for the second week, Thursday and later {return true;} return false ;}

When I wrote the second condition, I found that the time covered by the condition seemed to be a bit problematic. I 'd like to see your opinions.

How do you feel that this is mandatory for other people to do interview questions? Since the organization can be listed as 123, the implementation should not be a problem...

Install Carbon

Use Carbon \ Carbon;/*** verification date * @ param string $ date * @ return boolean */function isValidDate ($ date) {// $ date is the time of the week + time () before on the day before $ date = true if (Carbon: parse ($ date)-> format ('w ') = Carbon: now ()-> format ('w') & time () <Carbon: parse ($ date)-> subDay (1) -> hour (18)-> minute (0)-> timestamp) {return true;} // $ date is next week + time () to = true elseif (Carbon: parse ($ date)-> format ('w') = Carbon: now ()-> addWeek (1) after six o'clock P.M. this Thursday) -> format ('w') & time ()> Carbon: now ()-> startOfDay ()-> addDay (3)-> hour (18) -> minute (0)-> timestamp) {return true;} return false ;}

Simply change the subject'sIf statement return

Function isValidDate ($ date) {// 1. $ date is the week + time () before on the day before $ date = true // 2. $ date: + time () next week. it must be = true after six o'clock P.M. this Thursday. // 3. otherwise, false is returned. (Note: One week starts from Monday) $ orderTime = strtotime ($ date); $ now = time (); if (date ('W', $ orderTime) === date ('W', $ now) // Current week {// time () = true return $ now <strtotime (date ('Y-m-d 18:00:00 ', strtotime ("$ date-1 day");} if (date ('W', $ orderTime) ==( date ('W', $ now) + 1) // next week {// time () to = true return $ now> strtotime (date ('Y-m-d six o'clock P.M ', strtotime ('+ '. 4-date ('w '). 'Days ');} return false ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.