Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP - Convert date string to Twitter format
<?php
$return = '';
$date = strtotime($date);
$diff = time() - $date;
if ($diff < 86400) {
if ($diff < 3600) { // Output e.g. 35 minutes ago
$return = $mins = floor($diff / 60);
$return .= ' minute';
if ($mins > 1 || $mins === 0) $return .= 's';
$return .= ' ago';
}
else { // Output e.g. 5 hours ago
$return = $hours = floor($diff / 3600);
$return .= ' hour';
if ($hours > 1 || $hours === 0) $return .= 's';
$return .= ' ago';
}
}
else { // Output e.g. 1 Dec
$return = date('j M', $date);
}
return $return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.