Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Date to iCal date format for DTSTART

Is there an easy way to get the correct format for an iCal DTSTART using php date?

I need the format to look like: 20111008T110000 or 20111008 (that one is easy) if I don't have any time.

Does PHP date have a quick way to do this, specifically one that adds the time or removes it when needed?

like image 260
Nic Hubbard Avatar asked Oct 06 '11 18:10

Nic Hubbard


2 Answers

There isn't any native PHP function or date format that I'm aware of, so you'll need to create your own function. Something like this:

function getIcalDate($time, $inclTime = true)
{
    return date('Ymd' . ($inclTime ? '\THis' : ''), $time);
}

As hakre pointed out in a comment, the date formatter can't distinguish between a date with time and a date without time - you'll have to decide the logic behind that.

like image 114
Viktor Avatar answered Sep 21 '22 19:09

Viktor


date('Ymd\THis', time())

You can replace time() with your own timestamp.

like image 27
IOrlandoni Avatar answered Sep 20 '22 19:09

IOrlandoni