php string date return wrong date [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
My code:
echo "Strtotime : ";
echo date("Y-m-d",strtotime('9 March, 2015'));
echo "<br> mktime : ";
echo date("Y-m-d",mktime('9 March, 2015'));
echo "<br> Normal : ";
echo date("Y-m-d",'9 March, 2015');
I need output 2015-03-09

You can use DateTime with date_create_from_format, if the structure of the String is always the same:
$d = '9 March, 2015';
echo date_create_from_format('d F, Y', $d)->format('Y-m-d');
See a running example: https://3v4l.org/s8EHi

I strongly recommend using the DateTime class for this:
$date = DateTime::createFromFormat ("d M, Y", "9 march, 2015");
echo $date->format ("Y-m-d");
If you check the PHP manual for DateTime, you can get a whole lot of other really good tips on how to further take advantage of this class. Which makes working with dates in PHP a lot easier than with the old functions. ;)

if you want to show date - month - year you can apply this
<?php
echo date('Y-m-d');
?>

I think you should use this code to get exact date format.
$orig_date = "22/09/2016";
$date = str_replace ('/', '-', $orig_date);
echo date('Y-m-d', strtotime ($date));
See more date and time format example here

Related

PHP Date Parse into a new format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a function that prints out:
echo $post->EE_Event->primary_datetime()->start_date_and_time();
result:
21-02-15 08:00
But i want to display it as:
08:00 21st February, 2015
I need help with a function to grab the existing printed out date and reformat it.
I have tried a few snippets online, but nothing that worked.
Try this code:
$yourDate = '21-02-15 08:00'; // $post->EE_Event->primary_datetime()->start_date_and_time()
$date = DateTime::createFromFormat('j-m-y G:i', $yourDate);
echo $date->format('G:i dS F, Y');
Try it online:
http://ideone.com/a7EEK4
In PHP >= 5.3 we have excellent date API.
See more here:
http://php.net/manual/en/datetime.createfromformat.php
PHP's DateTime class has exactly what you need. You want to use the createFromFormat to parse it in, then use format to print it back out.
$myDate = $post->EE_Event->primary_datetime()->start_date_and_time();
$myDateObj = DateTime::createFromFormat('d-m-y G:i', $myDate);
echo $myDateObj->format('G:i jS F, Y');
echo date("G:i dS F, Y", strtotime($post->EE_Event->primary_datetime()->start_date_and_time()));

Formatting a date to a specific format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I have the following data available : 01/02/2014
What I need to format to is: 2014-01-02 00:00:00
What is the best way to do this in PHP?
$dt = DateTime::createFromFormat('m/d/Y', '01/02/2014');
echo $dt->format('Y-m-d 00:00:00');
See it in action
$val="01/02/2014";
echo date("Y-m-d H:i:s", strtotime($val));
Demo
First of all you should try it yourself.
you should study php date format documentation : http://www.php.net/manual/en/datetime.formats.date.php
Live demo:
https://eval.in/93149
$d = "01/02/2014";
echo date('Y-m-d H:i:s',strtotime($d));
OUTPUT:
2014-01-02 00:00:00
By using PHP date function
$date = "1st April 2000";
OR
$date = "01/04/2000";
echo date("Y-m-d H:i:s",strtotime($date));

PHP change format of date [duplicate]

This question already has answers here:
PHP date format conversion
(3 answers)
Closed 9 years ago.
Is it possible through PHP to change the format of this date
Jan 1 1900 12:00AM
to
01/01/1990
mm/dd/yyy
Two functions will help: strtotime and date.
So do this:
date("m/d/Y", strtotime("Jan 1 1990 12:00AM"))
See PHP's excellent DateTime classes:-
$date = DateTime::createFromFormat('M d Y H:ia', 'Jan 1 1900 12:00AM');
echo $date->format('m/d/Y');
Or, if you are a fan of one liners:-
echo DateTime::createFromFormat('M d Y H:ia', 'Jan 1 1900 12:00AM')->format('m/d/Y');
You could parse that into a timestamp with strtotime() and then use the PHP date() function to format it.
strtotime and strftime are the easiest to work with.
$stamp = strtotime('Jan 1 1900 12:00AM');
echo strftime("%m/%d/%G", $stamp);
See the full strftime documentation to output your date in any format.
No, because Jan 1 1900 1200AM is never going to equal 01/01/1990. Check you year. But if you want a simple what to convert string to time try this:
<?php
$d = 'Jan 1, 1900 12:00AM';
echo date("m/d/y",strtotime($d));
?>
I would pass this into a function first that removed the time element and then converts only the data portion.
see the date function:
//replace the time() with yours
echo date('m/d/Y', time()); // = 06/06/2013
manual: http://php.net/manual/de/function.date.php

php - convert 2012-06-25 19:00:00 to another format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
via xml, I am getting variables that give time in this format: 2012-06-25 19:00:00
I'd like it to be converted in this format: June 25 - 7:00PM
I have looked at mktime and strtotime, but im not sure how to handle a format that comes as a date AND time.
As you said, you can use strtotime:
echo date('F-m - h:iA', strtotime($old_format));
<?php
$input = "2012-06-25 19:00:00";
$dateTimeObject = new DateTime($input);
echo "First way: " . $dateTimeObject->format("F d - g:iA") . "<br>\n";
$date = date("F d - g:iA", strtotime($input));
echo "Second way: " . $date;

php date to timestamp [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert date to timestamp in PHP?
I want to transfer some date to timestamp ,the date formart is %day %month %time. Like 25 nov 07:44, I tried some method below, but the two dates are different. need a help, thanks.
<?php
$date = '25 nov 07:44';
$time = explode(" ",$date);
$minute = explode(":",$time[2]);
$timestamp = mktime(''.$minute[0].' '.$minute[1].' 00 '.$time[1].' '.$time[0].' 2011')."<br />";// mktime(%hour, %minute, %second, %month, %day, %year);
echo $timestamp; //1322202671
echo date("Y-m-j H:i:s", $timestamp); //2011-11-25 07:31:11
?>
A few problems:
You are feeding mktime a string instead of a comma separated list
$time[1] is a string and mktime needs a number for the month
You might want to give it a try with strtotime or otherwise add a translation table to go from your month strings to a number.
In recent versions of php (5.3+), you can use DateTime::createFromFormat().
Try something like
$format = 'd M H:i';
$date = DateTime::createFromFormat($format, $timestamp);
echo $date->format('Y-m-j H:i:s');
Personally, I find this class based approach a little cleaner.
PHP Docs:
DateTime::createFromFormat
date() for info on format strings

Categories