PHP Date to iCal date format for DTSTART - php

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?

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.

date('Ymd\THis', time())
You can replace time() with your own timestamp.

This is might be you are looking for link and this also can help you.
mktime function to make timestamp as you want including time or only date to it.

Related

Timestamp Difference and Output

In My SQL Database I have a Timestamp Column with values like this one representing the Date of the last edit:
2015-01-17 08:55:34.000000
I want to compare the Date with the current date and when is the same day I want to echo Today and otherwise I want to Display the Date of the last edit:
$timefromdb = '2015-01-17 08:55:34.000000'
$edit = strtotime($timefromdb);
if($edit > $_SERVER['REQUEST_TIME']){echo "Today";}
else{
echo strftime("on %A, the %d %B %Y", $edit);
}
echo " at ".date('h:i',$edit)
It always Displays 01/01/1970. There must be a Problem with strtotime. I did a bit of research and it seems like my Timestamp Format isn't a valid one: http://php.net/manual/en/datetime.formats.php
Around the web are a lot of Questions about converting Timestamps but I just can't find the right one: I also got a bit confused by all the functions to convert date stuff.
So can someone Tell me how to get a valid Timestamp for using it in strftime and to compare it to the REQUEST_TIME.
Thanks in Advance!
UPDATE: As Always: The Problem sits in Front of the PC. I declared the Variable but never assgined the Timestamp to it :)
Chop off the .000000 from the date as it makes the date a format strtotime() cannot work with. There's several ways to do this. A simple substr is one of them.
$timefromdb = substr('2015-01-17 08:55:34.000000', 0, -7);
I'm not exactly understood you, but
try
1. compare gettype( $edit ) and gettype($_SERVER['REQUEST_TIME'])
2. not sure what $timefromdb will be more then $_SERVER['REQUEST_TIME'], because IMHO when user edited data, time of it action will me less then current time.

Human readable date into timestamp

I am trying to parse a logfile, using human readable dates without a year into a timestamp. I have looked over the function strtotime() but haven't had any success with it.
Example time: Apr-26-10:49:36
which is the equiv of "M-j-H:i:s" for the date() function.
Edit: without a year, in my case here..its perfectly fine to assume the year is the current year.
I've created a script to break this down in the past, but it was long and redundant. I was hoping for a more simplified way of doing this. Any help, or pointing in the right direction would be greatly appreciated :D
If you know the format of the date, you can use strptime to parse it. This returns an array that you can use to determine the arguments for mktime.
Maybe not the prettiest way to do it but it works
The problem why the strtotime function is not working is because the hyphen between the Day and the Hour 26-10.
You can replace the - and then use strtotime.
echo date('Y-m-d H:i:s', strtotime(str_replace('-', ' ', 'Apr-26-10:49:36')));

Function to parse psql timestamp

I display the date or the time on my website a lot and I'm thinking about writing a function to parse a PostgreSQL timestamp.
The timestamp is in the format: Y-m-d H:i:s.u. E.g. 2011-04-08 23:00:56.544.
I'm thinking about something like this:
function parse_timestamp($timestamp, $format = 'd-m-Y')
{
// parse the timestamp
return $formatted_timestamp;
}
However I am wondering whether this can also be achieved without writing a parser for it myself (with the use of some PHP function).
function parse_timestamp($timestamp, $format = 'd-m-Y')
{
return date($format, strtotime($timestamp));
}
Don't forget to set timezone before, e.g.
date_default_timezone_set('UTC');
Or in your case, I guess 'Europe/Amsterdam'.
You can always get PHP timestamp of this format Y-m-d H:i:s.u using strtotime(). Then, using date() you can export time in your own format. Both functions depend of time zone set.
strtotime is perfectly capable of parsing that time string, then just reformat it with date:
echo date('d-m-Y', strtotime('2011-04-08 23:00:56.544')); // 08-04-2011
For those using DateTime class:
DateTime::createFromFormat('Y-m-d H:i:s.u', $yourTime);
If the database isn't giving you what you want, change it. PostgreSQL can also format dates and times.
select to_char(timestamp '2011-03-04 07:04:00', 'DD-MM-YYYY');
04-03-2011
But that's risky in international contexts, like the web. Different locales expect different ordering of elements. (Does "04-03" mean 03-April or 04-March?) This expression is easier to understand, and it uses locale-specific abbreviations for the months.
select to_char(timestamp '2011-03-04 07:04:00', 'DD-Mon-YYYY');
04-Mar-2011
Take a look at the strptime function - it can parse many time formats.

strtotime of today

Hallo, I want to find the difference between today and another date,
convert todays date into unix time format here
<?php
echo '1st one'.strtotime(date("d.m.Y")).'<br>';
echo '2nd one'.strtotime(date("m.d.Y")).'<br>';
?>
The first echo is producing some value, but not the second one. What is the bug in it...please help..
strtotime makes assumptions based on the date format you give it. For instance
date("Y-m-d", strtotime(date("d.m.Y"))) //=> "2010-09-27"
date("Y-m-d", strtotime(date("m.d.Y"))) //=> "1969-12-31"
Note that when given an invalid date, strtotime defaults to the timestamp for 1969-12-31 19:00:00, so when you end up with an unexpected date in 1969, you know you're working with an invalid date.
Because strtotime is looking for day.month.year when you use . as the delimiter, so it sees "9.27.2010" as the 9th day of the 27th month, which obviously doesn't exist.
However, if you change it to use / as the delimiter:
date("Y-m-d", strtotime(date("d/m/Y"))) //=> "1969-12-31"
date("Y-m-d", strtotime(date("m/d/Y"))) //=> "2010-09-27"
In this case, strtotime expects dates in month/day/year format.
If you want to be safe, Y-m-d is generally a good format to use.
It's worth pointing out that strtotime() does accept words like "today" as valid input, so you don't need to put a call to date() in there if all you want is today's date. You could just use strtotime('today');.
Come to think of it, a simple call to time(); will get you the current time stamp too.
But to actually answer the question, you need to consider that d.m.Y and m.d.Y are ambiguous - if the day of the month is less than the 12th, it is impossible to tell which of those two date formats was intended. Therefore PHP only accepts one of them (I believe it uses m/d/Y if you have slashes, but for dots or dashes it assumes d-m-Y.
If you're using strtotime() internally for converting date formats, etc, there is almost certainly a better way to do it. But if you really need to do this, then use 'Y-m-d' format, because it's much more universally reliable.
On the other hand, if you're accepting date input from your users and assuming that strtotime() will deal with anything thrown at it, then sadly you're wrong; strtotime() has some quite big limitations, of which you've found one. But there are a number of others. If you plan to use strtotime() for this sort of thing then you need to do additional processing as well. There may also be better options such as using a front-end Javascript date control to make it easier for your users without having to rely on strtotime() to work out what they meant.
strtotime does not consider 09.27.2010 to be a valid date...
You could check it like this:
<?php
// will return false (as defined by the docs)
echo var_dump(strtotime("09.27.2010"));
?>
The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp. US time format is : MM DD YYYY
look here for the Information about which formats are valid http://www.php.net/manual/en/datetime.formats.php. But what do you mean with deference between 2 dates? You mean the Timespan between 2 dates?
echo (time() - strotime("- 2 days")) . " seconds difference";
Something like that?
strtotime would not take the d.m.y format. good way is Y-m-d

How to format datetime most easily in PHP?

To change 2009-12-09 13:32:15 to 09/12/2009
here:
echo date("d/m/Y", strtotime('2009-12-09 13:32:15'))
You can use strtotime to get the timestamp of the first date, and date to convert it to a string using the format you want.
$timestamp = strtotime('2009-12-09 13:32:15');
echo date('d/m/Y', $timestamp);
And you'll get :
09/12/2009
[edit 2012-05-19] Note that strtotime() suffers a couple of possibly important limitations:
The format of the date must be YYYY-MM-DD; it might work in some other cases, but not always !
Also, working with UNIX Timestamps, as done with date() and strtotime() means you'll only be able to work with dates between 1970 and 2038 (possibly a wider range, depending on your system -- but not and illimited one anyway)
Working with the DateTime class is often a far better alternative:
You can use either DateTime::__construct() or DateTime::createFromFormat() to create a DateTime object -- the second one is only available with PHP >= 5.3, but allows you to specify the date's format, which can prove useful,
And you can use the DateTime::format() method to convert that object to any date format you might want to work with.
Using the date() method.
print date("d/m/Y", strtotime("2009-12-09 13:32:15"));
$long_date = '2009-12-09 13:32:15';
$epoch_date = strtotime($long_date);
$short_date = date('m/d/Y', $epoch_date);
The above is not the shortest way of doing it, but having the long date as an epoch timestamp ensures that you can reuse the original long date to get other date format outputs, like if you wanted to go back and have just the time somewhere else.

Categories