<?php
// first line of PHP
$defaultTimeZone = 'UTC';
include 'fun/db_config.php';
if (date_default_timezone_get() != $defaultTimeZone) date_default_timezone_set($defaultTimeZone);

// somewhere in the code
function _date($format = "r", $timestamp = false, $timezone = false)
{
	$userTimezone = new DateTimeZone(!empty($timezone) ? $timezone : 'GMT');
	$gmtTimezone = new DateTimeZone('GMT');
	$myDateTime = new DateTime(($timestamp != false ? date("r", (int)$timestamp) : date("r")), $gmtTimezone);
	$offset = $userTimezone->getOffset($myDateTime);
	return date($format, ($timestamp != false ? (int)$timestamp : $myDateTime->format('U')) + $offset);
}

/* Example */
//echo 'Today is : '._date("Y-m-d | h:i:sa",false,'Asia/Rangoon').'<br>';
//echo 'New York Date/Time: '._date("Y-m-d | h:i:sa", false, 'America/New_York').'<br>';
//echo 'Belgrade Date/Time: '._date("Y-m-d | h:i:sa", false, 'Europe/Belgrade').'<br>';
//echo 'Belgrade Date/Time: '._date("Y-m-d | h:i:sa", 514640700, 'Europe/Belgrade').'<br>';

?>
<?php

/*
*   Check Whather Record is already exist in table
*/
function isExist($table, $Callcolumn, $column, $value)
{
	$query = "SELECT  " . $Callcolumn . " FROM " . $table . " WHERE " . $column . " = ?";
$conn;
	if ($stmt = $conn->prepare($query)) {
		$stmt->bind_param("s", $value);
		$stmt->execute();
		$stmt->store_result();
		$stmt->fetch();
		if ($stmt->num_rows == 1) {
			$stmt->close();
			return true;
		}
		$stmt->close();
	}
	return false;
}

function isExistA($query)
{
	// $col = implode("=? AND ",array_keys($col_val)) . "=?";
	// $query = "SELECT aid FROM " . $table . " WHERE " . $col;
	// echo $query;
	global $conn;
	if ($stmt = $conn->prepare($query)) {
		$stmt->execute();
		$stmt->store_result();
		$stmt->fetch();
		if ($stmt->num_rows == 1) {
			$stmt->close();
			return true;
		}
		$stmt->close();
	}
	return false;
}

function print_param($params)
{
	echo '<ul>';
	foreach ((array)$params as $allowedMethod) {
		echo '<li>';
		echo $allowedMethod;
		echo '</li>';
	}
	echo '</ul>';
}

function saveImage($img, $directory)
{
	//$img = $input['image'];
	$img = str_replace('data:image/jpeg;base64,', '', $img);
	$img = str_replace(' ', '+', $img);
	$data = base64_decode($img);
	$file = $directory . uniqid() . '.jpg';
	$success = file_put_contents($file, $data);
	if ($success) {
		return $file;
	}
	return null;
}

function deleteImg($src)
{
    if (file_exists($src)) {
        unlink($src);
        return true;
    } else {
        return false;
    }
}

function getString($query)
{
	global $conn;
	$result = $conn->query($query);
	while ($row = $result->fetch_row()) {
		// temp user array
		return $row[0];
	}
}



function getNumber($query)
{
	global $conn;
	$n = 0;
	$result = $conn->query($query);
	if ($result->num_rows > 0) {
		while ($row = $result->fetch_row()) {
			$n = $row[0];
		}
	}
	return $n;
}

function get_All($Query)
{
    global $conn;
    $response = array();

    $result = $conn->query($Query);
    // check for empty result
    if ($result->num_rows > 0) {
        $response["result"] = array();
        while ($row = $result->fetch_assoc()) {
            array_push($response["result"], $row);
        }
        $response["status"] = 0;
        $response["msg"] = $result->num_rows . " records";
    } else {
        $response["status"] = 1;
        $response["msg"] = "No record found in table";
    }

    return $response;
}

// function jsonResponse($data, $code = 200)
// {
//     return response()->json($data, $code,
//         ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'], JSON_UNESCAPED_UNICODE);
// }
