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));
Related
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
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.
I am currently using PHP ROUND , ABS , STRTOTIME to calculate the difference between two dates.
The calculation works until you select a $_SESSION['b_checkout'] from a new year. i.e. if the b_checkin is Dec 30 and the b_checkout is Dec 31, this returns the correct $no_nights as 1 day.
$_SESSION['b_checkin'] and $_SESSION['b_checkout'] are using 'D F jS Y' date format. e.g.
$_SESSION['b_checkin'] = "Wednesday, 31 December, 2014"
$_SESSION['b_checkout'] = "Thursday, 1 January, 2015"
$no_nights = round(abs(strtotime($_SESSION['b_checkout']) - strtotime($_SESSION['b_checkin']))/(60*60*24));
Currently this outputs (echo $no_nights) 363 days instead of 1 day. What is the problem?
Remove all the commas and see it works!
$b_checkin = "Wednesday 31 December 2014";
$b_checkout = "Thursday 1 January 2015";
$no_nights = round(abs(strtotime($b_checkout) - strtotime($b_checkin))/(60*60*24));
echo $no_nights;
When php can't interpret the year correctly, it uses the current year for parsing. thats how you are getting 363 days as result.
Use below format for checkin and checkout
// 'F jS Y'
$_SESSION['b_checkin'] = "31 December, 2014";
$_SESSION['b_checkout'] = "1 January, 2015";
Hope this helps ;)
Cheers!!!
i was fetching this date from table in the database like this format
Sunday 16th of January 2011 06:55:41 PM
and i want to convert it to be like this format
11-05-2012
how to do that with date function or any function
when i use date function
<td><?php echo date('d-m-Y', $v['v_created']); ?></td>
i get error message
'Severity: Warning
Message: date() expects parameter 2 to be long, string given'
This works for me (just tested on local web server)
<?php
date_default_timezone_set ('Europe/Rome');
$date = "Sunday 16th of January 2011 06:55:41 PM";
//.Strip "of" messing with php strtotime
$date = str_replace('of', '', $date);
$sql_friendly_date = date('y-m-d H:i', strtotime($date));
echo $sql_friendly_date;
?>
You can format the date as you prefer changing the first parameter of Date function according to: http://it2.php.net/manual/en/function.date.php
You have the following format:
Sunday 16th of January 2011 06:55:41 PM
that is a string based format, so the date information is more or less encoded in a human readable format. Luckily in english language. Let's see, that are multiple things all separated by a space:
Sunday - Weekdayname
16th - Date of Month, numeric, st/nd/th form
of - The string "of".
January - Monthname
2011 - Year, 4 digits
06:55:41 - Hour 2 digits 12 hours; Colon; Minute 2 digits; Colon; Seconds 2 digits
PM - AM/PM
So you could separate each node by space and then analyze the data. All you need is all Monthnames and the sscanf function because you only need to have the month, date of month and year:
$input = 'Sunday 16th of January 2011 06:55:41 PM';
$r = sscanf($input, "%*s %d%*s of %s %d", $day, $monthname, $year);
Which will already give you the following variables:
$monthname - string(7) "January"
$day - int(16)
$year - int(2011)
So all left to do is to transpose the monthname to a number which can be done with a map (in the form of an array in PHP) and some formatted output:
$monthnames = array(
'January' => 1,
# ...
);
printf("%02d-%02d-%04d", $day, $monthnames[$monthname], $year);
So regardless of which problem, as long as the input is somewhat consistently formatted you can pull it apart, process the gained data and do the output according to your needs. That is how it works.
try this. always worked for me
$date = Sunday 16th of January 2011 06:55:41 PM
$new_date = date('d-M-Y', strtotime($date));
echo $new_date;
The format you are using Sunday 16th of January 2011 06:55:41 PM is a wrong format.from the form you are inserted this date in database should be in date(Y-m-d) than the value of date inserted in database like:- 11-05-2012. and you can fetch this and get the format what you want.
<?php
$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$new_date = date('d-M-Y', strtotime($old_date));
echo $new_date
?>
more details about date plz visit this url
http://www.php.net/manual/en/datetime.createfromformat.php
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();