I'm entering this date :
$user_entered_date = '30 November, 2020';
when i am change date format using this command
$new_user_entered_date = date( 'Y-m-d', strtotime($user_entered_date) );
then result is :
2018-11-30
Tell me what is the solution of this problem or where i'm wrong.
Your mistake is the comma ', ' you have to it without like this:
$user_entered_date = '30 November 2020';
You need to use createFromFormat if you have no control over the input format.
$user_entered_date = DateTime::createFromFormat('d M, Y', '30 November, 2020');
$new_user_entered_date = date_format($user_entered_date, 'Y-m-d');
echo $new_user_entered_date;
With the comma,it ignores the 2020 year and takes 30 November only
The date formed is hence in 2018,use without comma for your answer
Related
I am trying to change the date 13 December, 2016 using date('Y-m-d',strtotime('13 December, 2016')) and it gives me the result of 2017-12-13, what i am missing here
Actually that comma within the date strings create an issue while using the strtotime function you can replace it with str_replace or instead you can simply use DateTime::createFromFormat method so no need of using extra function like as
$date = DateTime::createFromFormat('d F, Y','13 December, 2016');
echo $date->format('Y-m-d');
or simply date_create_from_format
$date = date_create_from_format('d F, Y','13 December, 2016');
echo $date->format('Y-m-d');
date('Y-m-d', strtotime('13 December 2016'));
You need to remove your comma.
This will give the expected result.
Try this
$date = '13 December 2016';
echo date('d-m-Y', strtotime($date));
The m formats the month to its numerical representation there.
Mistake: You don't have to put coma there.
use
$time = strtotime('10/21/2016');
$newformat = date('Y-m-d',$time);
echo $newformat;
I'd like to use the datetime->modify function on a date string that's formatted like "21 Jan 2016". When I use the datetime->modify and add 1 day, it gives me a result of 30 Apr 2017. I know that if I don't use the short month name and use a number instead (i.e. 01), it will work fine but I would like to get it work this way with short month name. Is this possible?
Please see code below:
<?php
$date = "21 Jan 2016"; // this is my date string
$newdate = new DateTime($date );
$date2 = $newdate->modify('+1 day'); // add 1 day to date string
echo $date2->format("d-M-Y");
?>
RESULT is:
30-Apr-2017
RESULT WANTED
22-Jan-2016
The problem is that you are trying to create a DateTime object from a non-ISO format. That's that part that is not working.
Take a look at: http://php.net/manual/ro/datetime.createfromformat.php
You will need to have something like
DateTime::createFromFormat('d M Y', '21 Jan 2016');
Full example:
$tomorrow = DateTime::createFromFormat('d M Y', '21 Jan 2016')->modify('+1 day')->format("d-M-Y");
echo($tomorrow);
The format of the $date variable is incorrect. Off the top of my head, there are two easy ways to fix this:
Set $date = "Jan 21, 2016"
Set $date = "21-Jan 2016"
More options: https://secure.php.net/manual/en/datetime.formats.date.php
Your date format was wrong. That's all.
How can I format this date 01 August, 15 for mysql DATE field. So I need this 2015-08-01 format from 01 August, 15 this format in PHP. Tried following but not work
echo date('Y-m-d',strtotime('01 August, 15'));
It is because strtotime() does not understand what 01 August, 15 means. Try it:
var_dump(strtotime('01 August, 15')); // false
The 15 at the end is too ambiguous; it could be the day of the month or a short year.
The easiest way to make this work is probably to use DateTime::createFromFormat, like so:
$date = '01 August, 15';
$parsed = DateTime::createFromFormat('d F, y', $date);
echo $parsed->format('Y-m-d');
If you control the format of the date then you could also make it easier to parse. Formatting it like 01 August 2015 would work, for example.
First remove the , out of the date and then use the strtotime function.
So:
$date = "01 August, 15";
$date = str_replace(",", "", $date);
echo date("Y-m-d",strtotime($date));
hello guys i have date like this
25 March 2014 - 16:45
i want to convert this date in to this format how to do that please Help
2014-03-25 16:45:00
i try this $creation = date('Y-m-d H:i:s',strtotime('25 March 2014 - 16:45'));
but it's not work
i try this $creation = date('Y-m-d H:i:s',strtotime('25 March 2014 - 16:45')); but it's not work
The reason your code doesn't work is because '25 March 2014 - 16:45' is not in a format that strtotime() can parse.
strtottime() is good at handling a wide range of formats, but it can't cope with absolutely anything; there's just too much variation possible.
I suggest that you try PHP's DateTime class instead. This class has a method called createFromFormat() which allows you to specify the format of the incoming date string. This makes it easier for it to parse, and allows for formats that might not be recognised otherwise, like yours.
Try this:
$date = DateTime::createFromFormat('j F Y - H:i', '25 March 2014 - 16:45');
echo $date->format('Y-m-d H:i:s');
Use PHP DateTime
You have "-" in "25 March 2014 - 16:45" in the string so it not be able to read by DateTime
So work around would be
$str = "25 March 2014 -16:45";
$date = new DateTime(str_replace("-","",$str));
echo $date->format('Y-m-d H:i:s');
I've successfully tried the following with your string:
$a = strptime('25 March 2014 - 16:45', '%d %B %Y - %R');
$time = mktime(
$a["tm_hour"],
$a["tm_min"],
$a["tm_sec"],
$a["tm_mon"]+1,
$a["tm_mday"],
$a["tm_year"]+1900
);
$converted = date('Y-m-d H:i:s',$time);
Try this:
$creation = date('Y-m-d H:i:s',strtotime('25 March 2014 16:45'));
I attempted this:
$date_string = strtotime('6 Mar, 2011 23:59:59');
But I think PHP can't interpret that for some reason as it returned empty. I tried this:
$date_string = strtotime('6 Mar, 2011 midnight');
The above worked but I need it to be a second before midnight i.e. the last second of the day. How can I get strtotime to return this without changing the 6 Mar, 2011 part?
Hope this helps. I used this and it gives todays timestamp just before midnight. Counter intuitive.
$today_timestamp = strtotime('tomorrow - 1 second');
It works for me if I use March 6, 2011 23:59:59. Any chance of changing the input format?
Other than that, you could of course subtract 1 second from the timestamp. Note however that you need to use March 7:
$date_string = strtotime('7 Mar, 2011 midnight') - 1;
Why not use mktime?
mktime(23,59,59,3,6,2011);
If you're on PHP 5.3 or greater, you could use the DateTime class.
The createFromFormat function allows you to manually specify how to parse your input date string.
$date = '6 Mar, 2011 23:59:59';
$timestamp = DateTime::createFromFormat('d M, Y H:i:s', $date)->getTimestamp();