How to convert date to string DD MM YYY - php

I need to convert the date from 2020-11-01 to string 01 Nov 2020
So far I could achieved like this
$test = DateTime::createFromFormat('Y-m-d', '2020-11-01')->format('d m Y');
But the result is show 01 11 2020
How to make the month become Nov?

date('d M Y',strtotime('2020-11-01'));

you can use strtotime to get the time stamp and the use that time stamp to change format with date funtion
$timeStamp=strtotime("2020-11-01");
$res=date("d M Y",$timeStamp);

Related

Convert 2017-03-01 to March 1st 2017 format inPHP

How to Convert 2017-03-01 to March 1st 2017 format in PHP.
$mark_entry_last_date=$exam_dates['last_date'];
echo date('M j<\sup>S</\sup> Y', $mark_entry_last_date);
Above code outputs current date only but I need the date value for $mark_entry_last_date. How to get a result like March 1st 2017 for given date using escape characters. ?
date function expects second parameter to be timestamp (integer) not string, so you need to use strtotime:
echo date('M j<\s\up>S</\s\up> Y', strtotime($mark_entry_last_date));
Also you need to escape u for sup tag. Since u is format for displaying microseconds.
Try without tags and use strtotime()
echo date("F jS Y", strtotime("2017-03-01"));
// output March 1st 2017
For more http://php.net/manual/en/function.strtotime.php

PHP Change 24 hour datetime using strtotime

I am using strtotime for saving date like strtotime($_POST['end_date']) and this give me format 1441785600 that show very fine in google calendar but when I save string like
Thu Sep 03 2015 14:00:00 (24 hour format)
Data stop displaying in google calendar, I want to convert above sample (24Hours) using strtotime and convert it into something like that 1441785600
You may use something like this which currently doesn't give as you want, but may be useful-
$date = 'Sep 03 2015 14:00:00';
$parsedDate = date_parse_from_format('M d Y H:i:s', $date);
$time_stamp = mktime(
$parsedDate['hour'], $parsedDate['minute'], $parsedDate['second'],
date('n', strtotime($parsedDate['month'])), $parsedDate['day'], $parsedDate['year'],
date('I')
);
You could do:
$dateString = 'Thu Sep 03 2015 14:00:00 (24 hour format)';
echo strtotime(preg_replace('~\w+\s(.+)\s\(.+~','\1',trim($dateString)));

PHP Date Conversion to Timestamp

I am trying to convert the following string (or strings of this time) to timestamps:
Closing date: 02 Apr 15
Closing date: 06 May 15
My code is as follows:
$start_date = explode("Closing date: ", $string);
$start_date = DateTime::createFromFormat('DD M yy', $start_date[1]);
But when I try echoing $start_date->getTimestamp() it tells me
Fatal error: Call to a member function getTimestamp() on a non-object
Any idea on what I may be doing wrong? DD M yy seems like the right date format to use.
It should be:
$start_date = explode("Closing date: ", $string);
$start_date = DateTime::createFromFormat('d M y', $start_date[1]);
The format you need to pass to DateTime::createFromFormat() to help it parse your data is d M y. I extracted the significance of the letters from the documentation:
d and j: Day of the month, 2 digits with or without leading zeros (01 to 31 or 1 to 31);
F and M: A textual representation of a month, such as January or Sept (January through December or Jan through Dec);
y: A two digit representation of a year (which is assumed to be in the range 1970-2069, inclusive); Examples: 99 or 03 (which will be interpreted as 1999 and 2003, respectively).
You need to change 'DD M yy' a to a right date format "d M y"
$start_date = explode("Closing date: ", 'Closing date: 02 Apr 15');
$start_date = DateTime::createFromFormat('d M y', $start_date[1]);
var_dump($start_date)
object(DateTime)[3430]
public 'date' => string '2015-04-02 15:27:28.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/London' (length=13)
DD M yy is actually not the right format. Taken from the DateTime::createFromFormat docs:
D and l - A textual representation of a day - Mon through Sun or Sunday
through Saturday
Try doing a var_dump of $start_date before calling getTimestamp, I bet is's actually false:
Returns a new DateTime instance or FALSE on failure.
The format you need to use is d M y.
Why not simply convert date to timestamps format using strtotime
$string="Closing date: 02 Apr 15";
$start_date = explode("Closing date: ", $string);
print strtotime($start_date[1]);

Get current datetime using PHP

I want to add content to a MySQL table with current date and time.
When I insert the content to database it shows the correct date but the wrong time.
$date1 = date("Y-m-d H:i:s");
date_default_timezone_set('Asia/Kolkata');
$date1 = date("Y-m-d H:i:s");
Asia/calcutta has changed to Asia/Kolkata
[a] strftime(): Format a local time/date according to locale settings
[b] date : Format a local time/date
Examples
Consider following simple example:
<?php
print strftime('%c');
?>
Output:
Mon Apr 23 01:22:58 2007
You need to pass format such as %c to strftime() to print date and time representation for the current system. You can use following format characters:
%m - month as a decimal number (range 01 to 12)
%d - day of the month as a decimal number (range 01 to 31)
%Y - year as a decimal number including the century
You can see the complete format conversion specifiers online here
You can also use date() as follows:
<?php
print date('r');
print "\\n";
print date('D, d M Y H:i:s T');
print "\\n";
?>
Output:
Mon, 23 Apr 2007 01:29:56 +0530
Mon, 23 Apr 2007 01:35:14 IST

Timestamp to normal date format and reverse conversion using PHP

I have a timestamp as 2011-08-27 18:29:31. I want to convert it to 27 Aug 2011 06.29.31 PM. Also, I want to convert this format reverse back to the previous timestamp format.
How van I do this using PHP?
$converted = date('d M Y h.i.s A', strtotime('2011-08-27 18:29:31'));
$reversed = date('Y-m-d H.i.s', strtotime($converted));
Don't use date()! It`s too old function. In PHP v. 5.2 and more you should use date_format or DateTime::format object.
you can use the date_format() function
//Convert to format: 27 Aug 2011 06.29.31 PM
$converted_date = date_format('d M Y h.i.s A',strtotime($orig_date));
//Convert to format 2011-08-27 18:29:31
$converted_date = date_format('Y-m-d H:i:s',strtotime($orig_date));
Have you looked at the date functions in PHP, especially strftime (for formatting a timestamp) and strtotime
To convert from 2011-08-27 18:29:31 to 27 Aug 2011 06.29.31 PM:
echo date('d M Y, H.i.s A', strtotime('2011-08-27 18:29:31'));
To do the reverse:
echo date('Y-m-d H:i:s',strtotime('27 Aug 2011 06.29.31 PM'));
If that doesn't work, you may have to try:
$date = date_create_from_format('d M Y, H.i.s A', '27 Aug 2011 06.29.31 PM');
echo date_format("Y-m-d H:i:s",$date);

Categories