PHP date function to extract a raw timestamp? - php

I want to convert a human readable date time such as 20210419115522 to 2021-04-19 11:55:22 via PHP. How is it possible?
<?php
echo date('Ymd', 20210419115301); <------ Not the result I expect

ex: $time = strtotime("20210419115301");
echo date("Y-m-d H:i:s", $time);

Related

Convert Time stamp to readable format

I need to conver timestamp to 2016-07-12 format. This is what I tried.
$selectedDate=date('m/d/Y H:i:s', '1465430400000');
I got 08/23/48407 00:00:00 I need to conver it to 2016-07-12 format.
Please Note: Here the format m/d/Y H:i:s isn't the matter. I'm getting wrong date is the problem
Any suggetion would be appricieated.
It looks like your timestamp is 1000x what date() expects, so try first dividing it by 1000 (and then, of course, use the right date format):
$selectedDate = date('Y-m-d', 1465430400000/1000);
You can convert Date in Any format:
<?php $date1 = strtotime($old_date);
echo $date = date("y-M-d", $date1); ?>
Complete list of format options

Create a php date format using datestring

I have a string like 2012-11-08.
I want to convert this string into php date format with time too.
The output should be like
$smv = date('Y-m-d H:i:s', time());
print $smv // 2012-11-08 16:05:56 (If we consider India )
I tried
$day = '2012-10-08';
echo = date("Y-m-d H:i:s",strtotime($day));
But it is printing the date as 2012-10-08 00:00:00 (current time is not printed).
Try like this
$day = '2012-10-08'.date('H:i:s', time());
echo date("Y-m-d H:i:s",strtotime($day));

/Date(1341788400000+0100)/ to DD/MM/YYYY HH:MM

I have several dates being outputted into variables. They are formatted as follows:
/Date(1341788400000+0100)/
How would I go about formatting them using PHP into:
DD/MM/YYYY HH:MM
Thanks!
I ended up using the following, as the initial format was in milliseconds:
$date = 1341788400000+0100;
$date = ( $date / 1000 );
$date = date("d/m/Y H:m", $date);
$date = 1341788400000+0100;
echo date("Y/m/d H:m",$date);
Unless the +0100 is the actual time of the day (01:00) ?
First, you parse it, e.g. using strtok() http://php.net/manual/en/function.strtok.php
Then parse it as a number.
$seconds = intval($a)
Then format it using
date("Y/m/d H:m", $seconds)`.

how to convert 12h to 24h in php

I want to convert 12h time in php which is in this format: 05/31/2012 09:48 AM
to 24h time format : 2011-11-27 11:53:36
This is my code line that you can change:
$time= $_POST['time'];
You can use this:
// $time = 05/31/2012 09:48 AM
$time = $_POST['time'];
//output new time: 2012-05-31 09:48:00
$newTime = date("Y-m-d H:i:s", strtotime($time)) );
live example
Doc:
strtotime
date
http://psoug.org/snippet/Convert_12_to_24_hour_time_and_vice_versa_241.htm
You have to use strtotime like this:
$date="05/31/2012 09:48 AM";
echo date("Y-m-d H:i:s",strtotime($date));
go through below link for date functionality in PHP
http://php.net/manual/en/function.date.php
You can use strtotime and then date functions for that.
http://php.net/manual/en/function.date.php All the possible options are listed there.
The first argument to date is the format and the second is a timestamp (which you get with strtotime of the current date string you have).
date('Y-m-d H:i:s', strtotime($your_current_date_string));
The format is just off the top of my head and not exact. You can look up the format you need in the table in the link provided.

PHP, except date function, is there any function can decode date?

Except for date function, is there any function that can decode date? I think the real date should be in recent days, not a day in 2033.
<?php
$date = '1294605921000';
echo date("m-d-Y H:i:s", $date);
//11-08-2033 23:55:20
?>
No, but since that timestamp is likely in milliseconds instead of seconds, divide it by 1000:
echo date("m-d-Y H:i:s", floor($date / 1000));
The timestamp looks as though it has been generated by JavaScript, which uses milliseconds. If you divide the timestamp by 1000, you get the right date:
<?php
$date = 1294605921000/1000;
echo date("m-d-Y H:i:s", $date);
//01-09-2011 20:45:21
?>
I am assuming that this is an excel type date serial - but i dont recognise the example you put.
From the Manual http://uk.php.net/manual/en/ref.datetime.php
<?
function xl2timestamp($xl_date)
{
$timestamp = ($xl - 25569) * 86400;
return $timestamp;
}
?>
...
gmdate — Format a GMT/UTC date/time
gmstrftime — Format a GMT/UTC time/date according to locale settings
...
Or just take a look here... Old, but good PHP Manual

Categories