Re: [PHP] A relative date puzzle
[email protected] (Jim Giner)
| Newsgroups | php.general |
|---|---|
| Message-ID | <[email protected]> |
On 4/1/2013 2:32 PM, Mattias Thorslund wrote:
> On 4/1/13 11:15 AM, Mattias Thorslund wrote:
>> On 4/1/13 11:05 AM, Jim Giner wrote:
>>> I'm looking for some ideas on how to handle the following get a
>>> datetime value that is relative to a specific future date when
>>> presented with a partial day &time value.
>>>
>>> Specifically, I have an appl that requires some lengthy input
>>> involving days and times. I have streamlined the d/e effort so that
>>> it can be done entirely using the number keypad (if available), or
>>> else just with numeric entries. Works great, very quick (JS) and
>>> accurate. Basically the users type some numbers and end up with
>>> something like:
>>>
>>> Sat 08:00am or Fri 07:00pm etc.
>>>
>>> Does anyone have an idea on how I can convert this value to a true
>>> datetime value for my database updates, where the above value is
>>> relative to a specific date in the future? IE, the d/e takes place a
>>> few days before a certain upcoming date and I want my entry of "Sat"
>>> to translate to the Saturday following that certain date. This could
>>> take place one to two weeks prior to the start date, so just adding
>>> "next" to the value above won't work. I really need to incorporate a
>>> specific date in the solution.
>>>
>>> Thoughts anyone?
>>>
>>
>>
>> Use strtotime() to get a UNIX time stamp, then use date() to format it
>> as you like.
>>
>> $myDate = strtotime("wed 11:00 am"); //1365012000
>> echo date("Y-m-d H:i:s", $myDate); //2013-04-03 11:00:00
>>
>> On two lines for clarity.
>>
>> Cheers,
>>
>> Mattias
>>
>
> I'm sorry, I didn't read the last part of your question too closely.
>
> strtotime() should help you there as well. Just put the timestamp of the
> date to compare the string expression to as the second parameter.
>
> $relativeToDate = strtotime("2013-04-04"); //1365058800
> $myDate = strtotime("wed 11:00 am", $relativeToDate); //1365616800
> echo date("Y-m-d H:i:s", $myDate); //2013-04-10 11:00:00
>
> For more advanced date manipulation, there are the Date/Time functions
> but this is simple stuff. You can even do it OO-style with the DateTime
> class & Co.
>
> Cheers,
>
> Mattias
Thanks for this - I'll experiment and see if I have it. Basically I
never read the strtotime doc close enough to get past the word "now" in
the syntax. Obviously it is EXACTLY what I needed.