Convert Timestamp to Readable Date/Time PHP

How do i Convert timestamp into Human readable format (1 month ago)

Carbon is just an extension of the \DateTime Object, so you can do this.

$dateTime = new Carbon('-1 month', new \DateTimeZone('Asia/Kolkata'));
echo $dateTime->diffForHumans();

If you have a timestamp you can do this

$dateTime = Carbon::createFromTimestamp(
$myTimestamp,
new \DateTimeZone('Asia/Kolkata')
);
echo $dateTime->diffForHumans();

Is it possible to convert a timestamp to human readable date (in any language) with CodeIgniter 3.1.9

Date helper will not help you. Use the following function (also include conversion to local time):

function dateTimeToFrench($datetime, $format="l j F Y à H:i:s", $convertToLocalTime = false)
{
$english_days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
$french_days = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche');
$english_months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$french_months = array('janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre');

$datetime = new DateTime($datetime, new DateTimeZone('UTC'));
if ($convertToLocalTime) $datetime->setTimezone(new DateTimeZone('Europe/Paris'));
return str_replace($english_months, $french_months, str_replace($english_days, $french_days, $datetime->format($format) ) );
}

convert timestamp to readable date returns +40,000 year

You've got too many digits; you're including microseconds, but PHP wants seconds since 1970.

date_create_from_format('U', (substr('1444782970870', 0, 10)))->format('Y-m-d');

works for me

http://php.net/manual/en/datetime.createfromformat.php

Convert timestamp format in PHP

Simple

echo date('Y-m-d H:i:s', 1568717579);

// Utc current time seconds from 00:00:00 UTC on 1 January 1970
echo time();

// Current time date
echo date('Y-m-d H:i:s', time());

How to create human readable time stamp?

This number is called Unix time. Functions like date() can accept it as the optional second parameter to format it in readable time.

Example:

echo date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);

If you omit the second parameter the current value of time() will be used.

echo date('Y-m-d H:i:s');

Convert timestamp to date?

You need foreach(), couple of explode() and strtotime()

foreach($array as $arr){
$exploded = explode('-',$arr);
$finalTime = '';
foreach($exploded as $explode){
$timeString = trim(explode('(',$explode)[0]);
$finalTime .= (!empty($finalTime)) ? "-".date('d/m/Y', strtotime($timeString)) : date('d/m/Y', strtotime($timeString));
}
echo $finalTime;
echo PHP_EOL;
}

Output:- https://3v4l.org/77uLI OR https://3v4l.org/91QIh

convert unix timestamp to date in Php

The problem is your timestamp is in milliseconds and $date->setTimestamp uses seconds, you can fix this by dividing by 1000

$date = new DateTime();
$value = 1426023505154;
$date->setTimestamp($value/1000);


Related Topics



Leave a reply



Submit