stories/app/includes/classes/Post.php

73 lines
2.5 KiB
PHP
Executable File

<?php
class Post {
private $con, $sqlData;
public function __construct($con, $input) {
$this->con = $con;
if(is_array($input)) {
$this->sqlData = $input;
}
else {
// $query = $this->con->prepare("SELECT * FROM videos WHERE id = :id OR url = :url");
$query = $this->con->prepare("SELECT * FROM posts WHERE url = :url");
$query->bindParam(":url", $input);
$query->execute();
$this->sqlData = $query->fetch(PDO::FETCH_ASSOC);
}
}
public function getPostId() {
return $this->sqlData["id"];
}
public function getPostUrl() {
return $this->sqlData["url"];
}
public function getPostContent() {
return $this->sqlData["content"];
}
public function getPostAuthor() {
return $this->sqlData["author"];
}
public function getPublishedDate() {
$dateStr = $this->sqlData["published"];
// return date('M j Y g:i A', strtotime($rawDate));
$timestamp = strtotime($dateStr);
$day = 60 * 60 * 24;
$today = time(); // current unix time
$since = $today - $timestamp;
# If it's been less than 1 day since the tweet was posted, figure out how long ago in seconds/minutes/hours
if (($since / $day) < 1) {
$timeUnits = array(
array(60 * 60, 'h'),
array(60, 'm'),
array(1, 's')
);
for ($i = 0, $n = count($timeUnits); $i < $n; $i++) {
$seconds = $timeUnits[$i][0];
$unit = $timeUnits[$i][1];
if (($count = floor($since / $seconds)) != 0) {
break;
}
}
return "$count{$unit} ago";
# If it's been a day or more, return the date: day (without leading 0) and 3-letter month
} else {
return date('M j Y g:i A', strtotime($dateStr));
}
}
}
?>