Convert Time stamp to readable format - php

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

Related

wrong convert date() PHP

I used date() to convert 12H time to 24H using this code
$over = date("Y-m-d H:i:s", strtotime("2021-12-16 13:42:46 PM"));
echo $over;
but the output is this below:
1969-12-31 16:00:00
How to get rid of this, is this a bug? or my code?
sandbox
13:42:46 PM isn't 12h time format (PM is nonsense in 24h format), 01:42:46 PM is correct.
Just specify the correct date format (PM is not supported):
$over = date("Y-m-d H:i:s", strtotime("2021-12-16 13:42:46"));
echo $over;
Which date format is supported you can find in https://www.php.net/manual/de/datetime.formats.php

Parse Date in PHP

I need some help parsing a date in PHP to a date object. I thought this was 8601 format, but $date = DateTime::createFromFormat(DateTime::ISO8601, "2018-11-14T01:11:36.059Z") is not working.
Date Format is: 2018-11-14T01:11:36.059Z
Try This:
echo date('Y-m-d H:i:s', strtotime('2018-11-14T01:11:36.059Z'));
//Output
2018-11-13 20:11:36

Convert date format to dd-mm-yyyy hh:mm:ii (2018-08-05T06:22:30Z) in PHP

I have date format like this (2018-08-05T06:22:30Z). I need to convert it to dd-mm-yyyy hh:mm:ii format.
I have tried this so far:
$dt ="2018-08-05T06:22:30Z";
$dt1 = date('d-m-Y HH:mm:ii', strtotime($dt));
Output of above code is 05-08-2018 0202:0808:2222.
How can I convert it to dd-mm-yyyy hh:mm:ii format?
Your time format is doubling up the format letters - HH:mm:ii you are also confusing the Month (m) and the minutes (i), it should be H:i:s which gives...
05-08-2018 07:22:30
Change $dt1 to be
$dt1 = date('d-m-Y H:i:s', strtotime($dt));
You should check PHP Date to see the format parameters. The ones I changed in your code were as follows;
i - which gets you the minutes with leading zeros
s - which gets you the seconds with leading zeros

PHP date year format

I am having problems with dates in php- sometimes the date gets to us in d/m/y and other times its d/m/Y. I want to convert all dates to d/m/Y.
Working with my current dataset, how would I get 24/06/2015 from 24/06/15 using php?
So far I have tried :
$original_date = '24/06/15';
$new_date = date('d/m/Y', strtotime($original_date));
This brings back 01/01/1970
This is probably the most robust method:
$string = '24/06/15';
$date = DateTime::createFromFormat('d/m/y', $string) ?: DateTime::createFromFormat('d/m/Y', $string);
echo $date->format('d/m/Y');
createFromFormat returns false if you try to parse 24/06/2014 using the d/m/y format, so in that case you just retry with d/m/Y. You then get a DateTime object which you can format and output any way you like.
use the lowercase 'y'. See the PHP date manual.
$new_date = date('d/m/y', strtotime($original_date));
y = A two digit representation of a year
The problem is that the strtotime doesn't recognise the UK date format, so convert the format first then format the date.
Try this:
$original_date = "24/06/15";
list($date,$month,$year) = sscanf($original_date, "%d/%d/%d");
$date_convert = $year."-".$month."-".$date;
$new_date = date("d/m/Y", strtotime($date_convert));
echo $new_date;
Its wrong format of date you are using for strtotime.
Have a look at Date Formats
The correct code should have
$original_date = '15/06/24'; // Notice : its mm/dd/yy here
$new_date = date('d/m/Y', strtotime($original_date));

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.

Categories