<?php
date_default_timezone_set('Europe/Istanbul');
try {
     $db = new PDO("mysql:host=localhost;dbname=heysen_db;charset=utf8mb4", "heysen_db", "M6n3r?74n");
} catch ( PDOException $e ){
     print $e->getMessage();
}

if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
  $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}

function time_ago($datetime){

    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $time_arr = [
        'y' => 'yıl',
        'm' => 'ay',
        'w' => 'hafta',
        'd' => 'gün',
        'h' => 'saat',
        'i' => 'dakika',
        's' => 'saniye'
    ];
    foreach($time_arr as $k => &$v){
        if($diff->$k){
            $v = $diff->$k . ' ' . $v;
        }else{
            unset($time_arr[$k]);
        }
    }

    return $time_arr ? implode(', ', array_slice($time_arr, 0, 1)) . ' önce' : 'Az önce';
}


?>