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
Related
I have a query that inserts date in this format
$time = date("m-d-y");
But when I Fetch and output date it shows wrong date. real: October 2020 but outputs: January 1970
$time = $row1['time'];
$newDate = date('F Y', strtotime($time));
echo $newDate;
How do I output date in this format: 20 OCT, 2020
Try not to store date/time using varchar datatype. If you can change it, please do. However, if you can't change database structure, you can use date_create_from_format() to create a DateTime object from a custom format:
echo date_create_from_format('m-d-y', "10-29-20")->format('F Y');
Output:
October 2020
Edit: Change the format part to ->format('d M, Y') to match your desired format
Output:
29 Oct, 2020
I have the following code which gets the date parsed, the subtracts 1 month. This works perfectly.
$date = '22-05-2016';
print(date("Y-m-d 23:59:59", strtotime($date.' -1 months')));
// outputs 2016-04-22 23:59:59
There are times where I need to force the date to month end. For this I use Y-m-t instead of Y-m-d Which works perfectly.
$date = '22-05-2016';
print(date("Y-m-t 23:59:59", strtotime($date.' -1 months')));
// outputs 2016-04-30 23:59:59
The problem comes when the date that is parsed is actually the last day of that month. It then flips to end of the next month.
$date = '31-05-2016';
print(date("Y-m-t 23:59:59", strtotime($date.' -1 months')));
Actual Output 2016-05-31 23:59:59 (hasn't removed 1 month)
Desired Output 2016-04-30 23:59:59
EDIT: Fiddle example http://ideone.com/0fqlor
Try:
$date = '31-05-2016';
print(date("Y-m-d 23:59:59", strtotime($date.' last day of last month')));
and dig into strtotime possibilities ;)
Take a look here: http://php.net/manual/en/datetime.formats.relative.php
Last Note: -1 month only gets 30 days back in time. Therefore it will not work allways. Some kind of PHP stuff, i think ;)
strtotime is your friend. The last day of any "given" month can be retrieved using the verbal String last day of May 2015 - so, for easy input (guess you have a date-picker not a month-picker), convert the selected date to its month expression first:
date_default_timezone_set('Europe/London');
$to = '31-05-2016';
$month = date("M Y", strtotime($to));
$strtotime_expression = "last day of " . $month;
echo $strtotime_expression.": <br />";
print(date("Y-m-t 23:59:59", strtotime($strtotime_expression)));
http://ideone.com/YvjVeP
I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();
How to display the text representation of day like sunday monday to the desired date ... I am using the strtotime function to display the date, but how to add the text day side to the date below.
$r=27 July 2012;
echo $r;
$cmg = strtotime("$r");
$now = time(); //25 July 2012
$timeleft = $cmg-$now;
$daysleft = round((($timeleft/24)/60)/60); //probably...
echo " There are $daysleft days left!";
This outputs a date
27 July 2012
There are 2days left !
Desired output is
27 July 2012 Friday
There are 2days left!!
Put the date in this format YYYY-MM-DD then use the Date() function:
$myDate = '2012-07-25';
echo date('l', strtotime($myDate));
//Friday
Documentation: http://www.php.net/manual/en/function.date.php
I need to find a date after a certain number of weeks. For example, if the start date is Saturday, 31 October 2009, and if I choose 16 weeks then I need to find the date after sixteen Saturdays.
Thanks in advance.
You can use strtotime:
// Oct 31 2009 plus 16 weeks
echo date('Y-m-d', strtotime('2009-10-31 +16 week')); // outputs 2010-02-20
// next week
echo date('Y-m-d', strtotime('+1 week')); // outputs 2009-11-07
strtotime() is what you want: it takes an expression and turns it into a date object. Also see date():
$date = '31 october 2009';
$modification = '+ 16 weeks';
echo date('Y-m-d (l)', strtotime("$date")); //2009-10-31 (Saturday)
echo date('Y-m-d (l)', strtotime("$date $modification")); // 2010-02-20 (Saturday)
If you're using PHP 5.2.0 or later, you can use the DateTime class
<?php
$date = new DateTime('31 October 2009'); // This accepts any format that strtotime() does.
// Now add 16 weeks
$date->modify('+16 weeks');
// Now you can output it however you wish
echo $date->format('Y-m-d');
?>
Why not just use unix time, subtract 7 * 24 * 3600 * 16 from that and then convert that back into the date that you need.
This will help with the conversion back:
http://php.net/manual/en/function.date.php