PHP Twitter Style Date - php

I am wanting to format my date with a Twitter style format however all the examples only have the x hours ago. What I would like...if possible, is:
- X min ago
- X hours ago
- Yesterday
- Thursday (or other days)
- 17 December
X min/hour ago and yesterday are pretty self explanatory but the day should be used if the date is greater than today and yesterday and the day falls within the current week (week starting Sunday).
The 17 December is if the date is greater than any day in the current week.
I am hoping this can be done. Cheers

I Googled "php twitter date" and found the second result gave this script that does exactly what you want:
http://www.skidoosh.co.uk/php/create-twitter-like-date-formatted-strings-with-php/
You could then edit the output formatting to display the types of information your wish.

Related

Dates in PHP 4.4

I am working for this association in my school and I am migrating web applications since, as of today, they are still running in PHP 4.4... But, I would like to quickly implement some fixes before changing to a newer PHP version.
One of these changes is related to dates comparison. The problem is I don't have strtotime and stuff like that, they all came in PHP 5.x.
I have to check if a timestamp is in one of two time intervals. In concrete:
I have a timestamp for when a person adhered to the association, school year goes from September 1st of year N to July 31st of year N+1, and I must check if the timestamp (and also the current time) lies
Between September 1st of year N and January 31st of year N+1, or
Between January 1st of year N+1 and July 31st of year N+1
So the question is what is the best way to do this? I came up with a solution: create my own timestamps and then compare. To create a timestamp for year N, I multiply the number of years since epoch time (N - 1970) by the number of seconds in a year ( 365.25 * 24 * 60 * 60 ). Then to that timestamp I add the number of seconds to September 1st, to January 1st, and so on. Finally I compare the whole. Isn't there a better way?
You are able to use PHP's strtotime function in PHP 4.4:
http://php.net/manual/en/function.strtotime.php
strtotime
(PHP 4, PHP 5, PHP 7)
strtotime — Parse about any English textual datetime description into a Unix timestamp
Here is an online example where you can run strtotime in 4.4.9:
http://sandbox.onlinephpfunctions.com/code/38559a58820d08105f746691bf338c565c1ed4e0

PHP DateTime sub produces unexpected results

I have the following example of me subtracting the DateInterval from DateTimeImmutable
$dateA = new DateTimeImmutable('2016-06-30');
$dateB = new DateTimeImmutable('2016-05-31');
$dateInterval = new DateInterval('P3M');
// print 2016-03-30 as expected
echo $dateA->sub($dateInterval)->format('Y-m-d');
// print 2016-03-02 which i would expect 2016-02-29
echo $dateB->sub($dateInterval)->format('Y-m-d');
When I set the period to 'P8M' it works as expected. How it comes, it dosent works for february?
Ok, it's really simple (kind of). Each 'month' interval evaluates to the prior (or X number of prior) month's equivalent day. If there are more days in the current month, than the month being landed on, the excess overflows to the following month.
So if you have a date which is May 31, 2016 and want to subtract 3 month intervals, it will:
Go back 3 months (in the list of months, don't think days yet), resulting in 'February'
Then look for February 31st. This doesn't exist so bleed over to following month 2 days (2016 Febuary has 29 days, so 2 extra days)
Viola! March 2nd.
Go forward, lets say you're in May 31, 2016 and want to add one month
Go forward one month to June.
Look for June 31st, nope, 1 extra day, bleed over to July.
As expected, July 1st is the answer.
The lesson in this: Adding and Subtracting Month intervals sucks, is confusing, and can lead to non-intuitive results unless you've got your month calculation rosetta stone with you.
Explanation from the PHP Docs
Note:
Relative month values are calculated based on the length of months that they pass through. An example would be "+2 month 2011-11-30", which would produce "2012-01-30". This is due to November being 30 days in length, and December being 31 days in length, producing a total of 61 days.

Calculating first day Date of a week from a given week number

I have a question and can't find that specific answer.
In my application, my work year start on july 1 every years. So 52 weeks later, I start again on july first. What I want is a field that I enter let say week 32. I want to have the answer of what is the first day of that specified week and show it as date format. So in other words, if I put week 1 in the field, it will give me the result = July 1 and if I put Week 2 in the field, it will gives me july 8, etc. But I think that every year could change. So I need a calculation that will do this. Is it possible in php?
Tks for help
Seby
strtotime("July 1 2013 +".$monthNum." months");
something like this?

How to get year-sensitive calendar week ? date("W") gives back 52 for 1st of January

As the headline says, PHP's date("W") function gives back the calendar week (for the current day). Unfortunatly it gives back 52 or 53 for the first day(s) of most years. This is, in a simple thinking way, correct, but very annoying, as January 1st of 2012 is not calendar week 52, it's NOT a calendar week of the current year. Most calendars define this as week 0 or week 52 of the previous year.
This is very tricky when you group each day of the year by their calendar week: 1st of January 2012 and 31st of December 2012 are both put into the same calendar week group.
So my question is: Is there a (native) year-sensitive alternative to PHP's date("W") ?
EDIT: I think I wrote the first version of this question in a very unclear way, so this is my edit: I'm searching for a function that gives back the correct calendar week for the first day(s) of the year. PHP's date("W") gives back 52 for the 1st of January 2012, which is "wrong". It should be 0 or null. According to official sources, the first calendar week of a year starts on the first monday of the year. So, if the first day of a year is not a monday, it's not week 1 ! It's week 0. The wikipedia article says
If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year.
This becomes tricky as the last days of the year are also in week 52/53. date("W") does not divide into current year and previous year.
This solution converts the excess of december to week 53 and everything in january prior to week 1 to week 0.
$w=(int)date('W');
$m=(int)date('n');
$w=$w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w);
echo "week $w in ".date('Y');
2013-12-31 ==> week 53 in 2013
2014-01-01 ==> week 1 in 2014
2015-12-31 ==> week 52 in 2015
2016-01-01 ==> week 0 in 2016
And a small test run, so you can see for yourself ;-)
$id=array(25,26,27,28,29,30,31,1,2,3,4,5,6,7,8);
for($iy=2013;$iy<2067;++$iy){foreach($id as $k=>$v){if($k<7){$im=12;}else{$im=1;}
if($k==7){++$iy;echo '====<br>';}$tme=strtotime("$im/$v/$iy");
echo date('d-m-Y',$tme),' * * ';
//THE ACTUAL CODE =================
$w=(int)date('W',$tme);
$m=(int)date('n',$tme);
$w=$w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w);
//THE ACTUAL CODE =================
echo '<b>WEEK: ',$w,' --- ','YEAR: ',date('Y',$tme),'</b><br>';}--$iy;
echo '----------------------------------<br>';}
Is there a (native) year-sensitive alternative to PHP's date("W") ?
No, there isn't.
According to official sources, the first calendar week of a year starts on the first monday of the year.
I'm not sure what official sources you're referring to.
PHP's date("W") returns the week number according to ISO 8601. As an international standard, ISO 8601 counts as one of possibly many "official sources". If its definition of week numbers doesn't fit your application, you're free to use anything else you like.
If you use a non-standard definition of "first week of the year", or if you use an official source that's not widely recognized, expect to have to write your own function to replace date("W"). (I'm pretty sure you'll need to write a function.)
The date 2012-01-01 was a Sunday. ISO 8601, Wikipedia, and php agree that the ISO week number for 2012-01-01 is 52.
ISO 8601 doesn't define a week 0.
So, if the first day of a year is not a monday, it's not week 1 !
Neither ISO nor Wikipedia say that. ISO 8601 defines week number 1 as the week that has the year's first Thursday in it. For 2012, the first Thursday was on Jan 5, so week number 1 was Jan 2 to Jan 8. 2012-01-01 was in the final week of the previous year, in terms of ISO weeks.
If you want something different, you can play with arithmetic, division, and so on. (Try dividing date("z") by 7, for example.) Or you can store that data in a database, and have your weeks any way you like.
If you're dealing with accounting periods, I'd almost certainly store that data in a table in a database. It's pretty easy to generate that kind of data with a spreadsheet.
The text of data in a table is much easier to audit than the text of a php function, no matter how simple that function is. And the data is certain to be the same for any program that accesses it, no matter what language it's written in. (So if your database someday has programs written in 5 different languages accessing it, you don't have to write, test, and maintain 5 different functions to get the week number.)
$d = new DateTime('first monday january '.date('Y'));
echo $d->format("W");
Google brought me here, and I wanted to post the following to help others like me...
I am in the US, and use DayPilot, and it works as follows:
Week starts on Sun, not Mon.
Jan 1st is always Week 1.
If Jan 1st is not a Sunday, Week 1 is less than 7 days.
This all makes a lot of since to me!
Here is my PHP function to copy that behavior:
function ProperWeekNum($inDate)
{$outNum = $inDate->format('W');
//Make week start on Sunday
if ($inDate->format('D') == 'Sun') {$outNum++;}
//Fix begining of year
if (($outNum >= 52) && ($inDate->format('M') == 'Jan')) {$outNum = 1;}
//Fix WEEK #1 is 1-day (Sat)
else //...without this 2022 was off by 1 all year
{$jan1st = new DateTime($inDate->format('Y').'-01-01');
if ($jan1st->format('D') == 'Sat') {$outNum++;}
}
//Return without leading zero
return ltrim($outNum, '0');
}
I use the function as follows, so when I click on DayPilot, my custom popup's Week # always matches DayPilot's Week #:
$weeknum = ProperWeekNum($startdate);
if ($weeknum != ProperWeekNum($enddate))
{$weeknum .= '-'.ProperWeekNum($enddate);}
Probably won't help the OP, but hopefully it helps someone.

Using PHP to parse a Google Calendar XML - End date is off by a day

I am trying to make a webpage and have the next three events on a Google Calendar show up on the home page. I have been using this PHP (http://james.cridland.net/code/google-calendar.html) to access my XML feed and format it into HTML.
The problem I'm having is that for some reason a new day starts at 11am. For example if my Google Calendar has an event from 10am on the 20th of December that lasts an hour, my PHP output will show an event that starts at 10am on the 20th which ends at 11am on the 21st. Otherwise it is working fine.
I have set my time to local (New Zealand) time on my Google Calendar account, and in PHP using date_default_timezone_set("Pacific/Auckland");
The horrible line that calculates the finish date is
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+date("Z",strtotime($ns_gd->when->attributes()->endTime)));
where $dateformat is a string with the date format.
The Google Calendar XML gives a start and finish time of
2011-12-22T10:00:00.000+13:00
2011-12-23T11:00:00.000+13:00
respectively, and the PHP is calculating a timeframe of 10.00am 22 December 2011 to 2.00pm 23 December 2011.
Whats going on?!?!
This line is indeed horrible:
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+
date("Z",strtotime($ns_gd->when->attributes()->endTime)));
strtotime can handle this type of ISO 8601 dates just fine. This code-fragment is probably written under the assumption that strtotime dismisses the timezone and returns the datetime in UTC and therefore the timezone "correction" needs to be calculated manually - that's what the +date("Z", ...) stands for (with "Z" the second parameter - the timestamp - is actually ignored).
So in your example 13 hours are added to your dates. And 10:00 + 13:00 = 23:00 (11 pm) which is still on the same day, but 11:00 + 13:00 = 24:00 (12 am) which is actually 00:00 on a new day.
So the correct way to convert the date is:
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime));
Try the zend framework for google calendar(It worked for me better than reinventing the wheel): http://framework.zend.com/manual/en/zend.gdata.calendar.html (look at the examples, they're quite easy and helpful)

Categories