I have a date format but still on String like this :
$date = '24-01-2017' # (dd-mm-yy)
I want to display them into datetime in Indonesian format
Selasa, 24-01-2017
In english
Tuesday, 24-01-2017
So, I created like this :
$date_format = DateTime::createFromFormat(' j-M-Y', $date);
echo $date_format->date('d-M-Y')
Not works
You should be using the format() function.
$now = new DateTime();
echo $now->format('Y-m-d');
// or
$now = DateTime::createFromFormat('d-m-Y', $date);
echo $now->format('Y-m-d');
Change the format to whatever you please.
To get the day in your own language:
$days = array(
'0' => 'Mon day',
'1' => 'Tues - Day',
// ... etc
);
You can now do:
$day = $now->format('n');
echo $days[$day].', '.$now->format('d-m-Y');
From a website that I found by doing a search for 'Date Time in Indonesian format, php'
PHP : Displaying the date and time in indonesian language
By this time I hope you can create your own date and time using the timestamp
of the current date or your own timestamp.
I use Indonesia language and I want to
display the date in Indonesian language. How can I do it? That's a perfect
question and asked by most beginners.
Let's create an associative array of date and month in our own language.
$days = Array ("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jum'at", "Sabtu");
$months = Array (1=>"Januari", 2=>"Pebruari", 3=>"Maret", 4=>"April",
5=>"Mei", 6=>"Juni", 7=>"Juli", 8=>"Agustus", 9=>"September",
10=>"Oktober", 11=>"Nopember", 12=>"Desember");
After we create our own language of date and months, now it's time to display it.
print $days[date("w")]; // display name of day with our own language
print $months[date("n")];
From php.net :
F and M : A textual representation of a month, such as January or Sept January through December or Jan through Dec
m and n : Numeric representation of a month, with or without leading zeros 01 through 12 or 1 through 12
You have to use "m" and not "M"
BTW, the method is not "date" but "format". ;)
<?php
$date = '24-01-2017';
$date_format = DateTime::createFromFormat('j-m-Y', $date);
echo $date_format->format('D d-m-Y');
?>
This outputs :
Tue 24-01-2017
Related
I have date in this type of format: April 1st 2017 and I want to convert it into this type of format: 2017/04/01 in my CodeIgniter code using php. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Your format can be used in the constructor of DateTime. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat(), you have to use the proper format
"F jS Y"
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit
April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F - full textual representation of a month, such as January.
d - day
S - English ordinal suffix for the day of the month
Y - 4-digit representation of year
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>
I am passing a date (dd/mm/yyyy) in URL with the following format:
http://www.website.com/_parameter=20/02/2000
I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$D->query = '';
if ($this->param('_parameter')) $D->query = date('Y-m-d', strtotime($this->param('_parameter')));
?>
And my database as following:
SELECT idtask FROM task WHERE unfinish=1 AND date LIKE '%".$D->query."%' "
The above return the following:
1970-01-01
When using strotime() you need to make sure you are using a valid datetime format. Otherwise strtotime() will return false or give you an unexpected value.
Dates that use XX/XX/XXXX format are assumed to be in US format which means the first two digits represent the month, the next two digits represent the day of month, and the last four digits represent the year. When dashes are used, the dates are assumed to be in European format. For example:
04/18/2017 = April 18, 2017
12/18/2017 = December 18, 2017
18-04-2017 = April 18, 2017
18-12-2017 = December 18, 2017
If you accidentally switch the day and month strtotime() will return false as the date is invalid.
18/04/2017 // error
18/12/2017 // error
04-18-2018 // error
12-18-2017 // error
The above examples are straight forward. But you can run into issues when the dates can be ambigous. For example:
04/12/2017 = April 12, 2017
12/04/2017 = December 4, 2017
04-12-2017 = December 4, 2017
12-04-2017 = April 12, 2017
In the above examples by switching the day and month we still get valid dates which can cause unexpected results in your application. To solve these potential issues it is recommended to use DateTime::createFromFormat() to parse the date ad return a DateTime() object from which you can get a Unix Timestamp, convert the date into another format, or use it to compare to other DateTime objects.
// Parse US date format
$date1 = DateTime::createFromFormat('m/d/Y', '04/18/2017');
// Get Unix timestamp of 1493581268
$timestamp = $date1->getTimestamp();
// Parse European date format
$date2 = DateTime::createFromFormat('d-m-Y', ''18-04-2017);
// Get MySQL format (ISO-8601) of 2017-04-18
$mysqlDate = $date2->format('Y-m-d');
See also:
Compare DateTime objects with comparison operators in PHP
For your specific case, the follow code will work:
$date = $date1 = DateTime::createFromFormat('m/d/Y', '20/02/2000');
$D->query = $date->format('Y-m-d'); // 2000-02-20
I have an API that returns a date string like this : 190416102906
Which means : 19 April 2016 at 10:29:06.
I want to store it in my Mysql Datetime but I don't know how to format it. It need to go from 190416102906 to 2016-04-19 10:29:06.
I've tried to use something like this but I probably didn't understand how it works :
$date_reponse = '190416102906';
$date = DateTime::createFromFormat("dmYHis",$date_reponse);
$newDate = date_format($date, 'Y-m-d H:i:s');
Because your format was incorrect : "dmYHis"
Y = Year format with 4 digit.
use y year with 2 digit (dmyHis)
$date = DateTime::createFromFormat("dmyHis",$date_reponse);
You just got the format the wrong way round and you need a lower case y for a 2 char year
$date_reponse = '190416102906';
$date = DateTime::createFromFormat("dmyHis",$date_reponse);
$newDate = date_format($date, 'Y-m-d H:i:s');
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 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