New date format from Twitter - php

So, I am implementing twitter via twitter widget pro into a wordpress theme. I'm wanting to date the tweets differently from wordpress and from the widgets formats. I want to follow twitters date format of 5h, 23h, or 3 Mar. All I've been able to find is how to do the "created _ ago" "about _ old" etc... I just want to use the xh if within 24 hours, otherwise use the day and month abbreviation. I really don't know php or too much js, so any help is very very much appreciated.

Use PHP DateTime and DateInterval classes,
$dt1 = new DateTime("2012-02-10 10:32:22");
$dt2 = new DateTime();
$diff = $dt2->diff($dt1);
echo $diff->format("%Y years, %m months, %d days, %H hours, %I minuts, %s seconds ago");
/// 00 years, 1 months, 18 days, 08 hours, 26 minuts, 51 seconds ago
If you dont want to echo 00 years, 00 months etc then check DateTime::y, DateTime::m etc properties is 0. if zero dont echo them. For example,
$format = "";
if ($diff->y > 0){
$format.="%Y years,";
}
if ($diff->m > 0){
$format.="%m months,";
}
...
// more conditions for hours, minutes, seconds
// echo the format
echo $diff->format($format);
Links of Interest,
DateTime::__construct()
DateTime::diff()
DateInterval::format()

Related

How to display days and hours with date() in php

I have a timestamp which calculates remaining time between 2 dates.
$job_expiration = strtotime($job['job_expiration']) - time(); like so.
Right now, I'm showing only 1 day or 2 days with the date() function.
date('j', $job_expiration) like so.
I also want to show hours instead of 2 days. let's say the time remaining is 1 day 16 hours or only 30 minutes. then I want to show the hours with day too.
1 day, 16 hours or only 30 minutes
how can I do that?
function convert_seconds($seconds) {
$dt1 = new DateTime("#0");
$dt2 = new DateTime("#$seconds");
return $dt1->diff($dt2)->format('%a days, %h hours, %i minutes and %s seconds');
}
echo convert_seconds(200000)."\n";
Sample output:
2 days, 7 hours, 33 minutes and 20 seconds
In your case you should use this function like convert_seconds($job_expiration)

How to check if the difference between 2 dates is bigger than 15 minutes?

I am using this function to compare a date from the database to the current date, and i need to check if the difference between the 2 dates is bigger than 15 minutes but i don't know how to do that, i think i need to do something like if($comp > 0 days 0 hours 15 minutes)
function TimeOut($dateP){
$date = new DateTime(date('Y-m-d H:i:s'));
$date2 = new DateTime($dateP);
echo $comp = $date->diff($date2)->format("%d days %h hours and %i minuts %s seconds");
if ($comp > "15 minutes ?") {
return true;
}
}
You can use diff and then read the m parameter of the result. In the example below $difference will be DateInterval object:
$difference = $start_date->diff($date2);
if($difference->i > 15) {
echo "difference greater than 15 minutes"
}
A date interval stores either a fixed amount of time (in years,
months, days, hours etc) or a relative time string in the format that
DateTime's constructor supports.

Display Days/Hours Until Certain Time

I need to calculate the remaining time (days/hours) until a certain date/time.
However, I'm not using a static date.
Imagine I have an event at 17:00 hrs on every Sunday. I need to display the time remaining until the next event, i.e. the oncoming Sunday 17:00.
I've found the following code in this answer. It works for a static date/time, but obviously isn't what I'm looking for.
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Thanks for your time.
You can use the relative time format next Sunday 17:00. Like this:
$now = new DateTime();
$future_date = new DateTime('next Sunday 17:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Output:
6 days, 2 hours, 33 minutes, 53 seconds
Read more about relative time formats here: http://www.php.net/manual/en/datetime.formats.relative.php

PHP - Formatting a Date

I have a date stored into a string variable as such: Sun Jun 02 08:54:12 EDT 2013. How can I convert this into a date variable to compare the time difference between the current date and the date provided. Thanks!
Use the DateTime class to compare the current date with the timestring. Once you have the DateTime objects you can easily get the difference using the method DateTime::diff(). It will return a DateInterval object that can be printed using it's format() method.
$dt = new DateTime('Sun Jun 02 08:54:12');
$now = new DateTime();
if($now > $dt) {
$difference = $now->diff($dt);
echo $difference->format('The time is %H hours %I minutes %S seconds in the past');
} else if ($now < $dt) {
$difference = $dt->diff($now);
echo $difference->format('The time is %H hours %I minutes %S seconds in the future');
} else {
echo 'the time is now';
}
Note: Of course you'll have to extend the output in a way that it displays the difference in years, months and days additionally. I didn't that in the example, because I'm lazy ... aehhmm because the string would get too long for the example ... ;)

Count total month

I have to find total months between two dates in Unix timestamp formats. I want to create a PHP function for that. I've tried this:
get_total_month($startunixdate, $endunixdate) {
$monthdiff = $endunixdate-$startunixdate;
$monthdiff = $monthdiff / 60*60*24*31;
return $monthdiff;
}
Does this function consider leap years as well as month with 30 and 31 separately, or it will just count an approximate month?
Your answer is in this line;
$monthdiff = $monthdiff / 60*60*24*31
This will just count a month based on 31 days. The code divides the seconds by 60 to get the number of minutes, 60 again to get hours, 24 to get number of days, then it uses 31 as the length of a month.
This will result in an approximation of the number of months. For instance, if you take timestamps at the beginning and end of February (start of March), you will usually have a timestamp difference equivalent to 28 days (29 in a leap year). Dividing that by 31 will give you a fraction less than 1, since you are using 31 to represent a full month, when in reality a whole calendar month has passed.
In order to do this, use the DateTime class.
function get_total_month($start, $end) {
// Create DateTime objects
$dt1 = new DateTime($start);
$dt2 = new DateTime($end);
// Get DateInterval object representing difference between the two
$diff = $dt1->diff($dt2); // OR: $diff = date_diff($dt1, $dt2);
// Print the "months" out
echo $diff->format("Difference: %R%m months"); // OR access $diff->m manually
U can use PHP 5.3 datetime method.
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Reference: PHP DateTime

Categories