PHP timestamp format [duplicate] - php

This question already has answers here:
How to parse a date string in PHP?
(2 answers)
Closed 1 year ago.
I never thought this will be such a pain. What is the best way to convert back this timestamp to a standard PHP timestamp that strtotime() accepts? :
September 29, 2017, 7:16 UTC
The format is:
date("F j, Y, G:i T")

Build a DateTime object first, then format it to a standard format, this will pass into strtotime.
$date = "September 29, 2017, 7:16 UTC";
$day = new DateTime($date);
$utc = strtotime($day->format("Y-m-d g:i A"));

Related

D d/m/y Convert to another format PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Date in a URL dd/mm/yyyy
(1 answer)
Closed 4 years ago.
I have date stamps from external source as per below format
Tue 6/02/2018
However when trying to convert the date to Y-m-d for SQL format the date get skewed, using the below;
date("Y-m-d H:i:s", strtotime('Tue 6/02/2018');
//ends up being Y-d-m
I have set the timezone earlier using
date_default_timezone_set('Australia/Brisbane');
strtotime() cannot parse the format "Tue 6/02/2018" correctly. You have to use DateTime::createFromFormat() to parse from your format.
$time = 'Tue 6/02/2018';
$date = DateTime::createFromFormat("D j/m/Y", $time) ;
echo $date->format("Y-m-d H:i:s") ; // 2018-02-06 07:12:19
You also could use new DateTimeZone() as:
$time = 'Tue 6/02/2018';
$tz = new DateTimeZone('Australia/Brisbane');
$date = DateTime::createFromFormat("D j/m/Y", $time, $tz) ;
echo $date->format("Y-m-d H:i:s") ; // 2018-02-06 17:17:48

Date convert showing wrongly [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I like to convert a date format dd/mm/yyyy to another format in PHP.
My Code:
$date = '29/01/2018';
echo date('l jS F Y', strtotime($date));
When I run the above code, it's showing me some wrong date:
Thursday 1st January 1970
Am I doing anything wrong?
You could also use the DateTime objects to help you converting dates. You can "create a datetime object from an specified format" and convert it.
$date = "29/01/2018";
$dt = DateTime::createFromFormat("d/m/Y", $date);
echo $dt->format("l jS F Y");
signifies American M/D/Y formatting
<?php
$date = '01/29/2018';
echo date('l jS F Y', strtotime($date));
output
Monday 29th January 2018

convert date format in php like 6th of November 2017 to 2017-11-6 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
i want to convert a date format to another using php
6th of November 2017 to 2017-11-6
i tried
$newDate = date("Y-m-d", strtotime('6th of November 2017'));
but it always returns 01-01-1970
When you create date from specific format then use createFromFormat of date.
$date = DateTime::createFromFormat('jS \o\f F Y', '6th of November 2017');
echo $date->format('Y-m-d');
Note: If you have string in your format then you have to escape that string (with \) for conversation
DEMO
Replace 6th of to 6th and it will be fine..
$newDate = date("Y-m-d", strtotime('6th November 2017'));
echo ($newDate);
result
2017-11-06
Try this:
$newDate = date("Y-m-d", strtotime("6 November 2017"));

PHP - Convert this string to another date/time format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I'm working with an API that returns timestamps in this format:
07/23/2017 23:39:21
The format is: MM/DD/YYYY HH:MM:SS
I need to covert this string so it displays in this format on my webpage:
23 July 2017, 11:39pm
Is this possible?
You can do this :
$date = new DateTime('07/23/2017 23:39:21');
echo $date->format('d F o, h:ia');
Output :
23 July 2017, 11:39pm
Take a look at http://php.net/manual/de/datetime.createfromformat.php. You'll get a DateTime object which you can convert in the format you desire.
Like so:
<?php
$date = DateTime::createFromFormat('m/d/Y H:i:s', '07/23/2017 23:39:21');
echo $date->format('d M Y, h:ia');
?>

How to convert date format using php? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
How to convert date format using php ?
i tried to convert from 2015-02-24 03:21:56 to Feb 24, 15 03:21:56
i tried this code but not work, How can i do ?
<?PHP
$date = "2015-02-24 03:21:56";
echo $new_date = date('F j, Y, g:i a', $date);
?>
The problem is that you're trying to feed a text string into date(), when really it wants a Unix timestamp. So, you need to convert your input data to a Unix timestamp first, then feed that to date().
Here is the code to do what you want:
<?php
$date = "2015-02-24 03:21:56";
$converted = strtotime($date);
$new_date = date('F j, Y, g:i a', $converted);
echo $new_date;
?>

Categories