Convert one date format into another in PHP
The second parameter to date()
needs to be a proper timestamp (seconds since January 1, 1970). You are passing a string, which date() can't recognize.
You can use strtotime() to convert a date string into a timestamp. However, even strtotime() doesn't recognize the y-m-d-h-i-s
format.
PHP 5.3 and up
Use DateTime::createFromFormat
. It allows you to specify an exact mask - using the date()
syntax - to parse incoming string dates with.
PHP 5.2 and lower
You will have to parse the elements (year, month, day, hour, minute, second) manually using substr()
and hand the results to mktime() that will build you a timestamp.
But that's a lot of work! I recommend using a different format that strftime() can understand. strftime() understands any date input short of the next time joe will slip on the ice
. for example, this works:
$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);
Convert a date format in PHP
Use strtotime()
and date()
:
$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));
(See the strtotime and date documentation on the PHP site.)
Note that this was a quick solution to the original question. For more extensive conversions, you should really be using the DateTime
class to parse and format :-)
Converting a formatted date to another in PHP
Here we are changing date from one format to another. Date format reference
Try this code snippet here
<?php
ini_set('display_errors', 1);
$date=date_create_from_format("l dS F, Y", 'Tuesday 11th October, 2016');
$newDate= $date->format("Y-m-d");
echo $newDate." 23:59:59";
Output: 2016-10-11 23:59:59
Convert Date Format in Different Format PHP
You could use the DateTime format method:
$incoming_date = '12/12/2020 23:18';
$date = DateTime::createFromFormat('d/m/Y H:i:s', $incoming_date . ':00');
$outgoing_date = $date->format('Y-m-d H:i:s');
I'm about 95% sure you can also leave off the seconds, but I can't say for sure without testing it:
$incoming_date = '12/12/2020 23:18';
$date = DateTime::createFromFormat('d/m/Y H:i', $incoming_date);
$outgoing_date = $date->format('Y-m-d H:i:s');
Using php date format to another format?
Your format is custom and not parsable by strtotime
. You have to make it parsable. Here is how you can do it(DEMO):
$checkin = "Tue, 2 October, 2018";
$checkinDate = explode(',',$checkin);
$strDate = $checkinDate[1].$checkinDate[2];
echo date("Y-m-d", strtotime($strDate));
This code breaks the string at ,
and then joins the last 2 parts ignoring the day.
Or you can use date_create_from_format()
, to parse this format, like(DEMO):
$checkin = "Tue, 2 October, 2018";
$checkin = date_create_from_format('D, j F, Y', $checkin);
echo date_format($checkin, "Y-m-d");
Convert a date into a specific format using PHP
$date = "06.11.2017 um 18:30 - 21:30 Uhr";
$date = explode(" ",$date);
if(array_key_exists(0,$date))
echo date("Y-m-d", strtotime($date[0]));
output: 2017-11-06
- Explode the string
- Convert the first part to the desired format.
Change date and time formate in PHP [advance way]
Personally I would make sure the date is being saved in simplier format to database (DATETIME
), then it's more accessible and even easy to use in database query when comparing dates etc.
But if you have no choice, this should do:
$orgDate = "Thursday 09th of July 2020 12:24:23 PM";
$dateTime = DateTime::createFromFormat('l dS * F Y h:i:s A', $orgDate);
echo $dateTime->format('Y-m-d');
DateTime parameters are explained in documentation: https://www.php.net/manual/en/datetime.createfromformat.php
How to convert date to string date in php?
You need to use M
here for month and d
for day:
$date=date_create("2019-04-18");
echo date_format($date,'d M Y'); // 18 Apr 2019
According to PHP Manual:
d
=> Day of the month, 2 digits with leading zeros (e.g 01 to 31)
M
=> A short textual representation of a month, three letters (e.g Jan through Dec)
Related Topics
How to Download an Mp3 File from Remote Url
How to Get the Sucess Message in the Same Page After Submitting the Contact Form
Php to View Pdf on Web Page and Disable User to Download
Unset a Value in a Multi-Dimensional Array Based on One of the Values
Sql Get an Id from a Column Where Ids Separated by Commas
How to Submit Multiple PHP Forms While Each Form Has Its Own Submit Button
How to Get the MAC and the Ip Address of a Connected Client in PHP
Executing Python Script in PHP and Exchanging Data Between the Two
How to Get All Month Record Count in Laravel
How to Get Previous Month from Selected Month
How to Add Auto Numbering With Each Fetching Row
[Php][Mysql] How to Insert Data in 2 Tables At Same Time
Calculate Size in Bytes of Json Payload Including It in the Json Payload in PHP