I have to convert a string input to date "1990/07/22" to 22/07/1990 as date,My function is as follows as:
public function($date){
$date_format_sec = strtotime($date);
$date_of_birth = new \DateTime('#'.$date_format_sec);
}
It uploads date as 21/07/1990 since I didn't give time.How to get the exact date given as same as input.
You can format your date with php
$formatedDate = $date_of_birth->format('d-m-Y');
Documentation
$input = '1990/07/22';
$date = \DateTime::createFromFormat('Y/m/d', $input)->format('d-m-Y');
As I said, you don't need to use strtotime, if the $date string is a valid date, the class DateTime will read it. After that you can use format.
In this example I set the format to be always the one you expect, while you can put any other format accepted by it.
public function getMyDate($date, $format = 'd/m/Y') {
$date_of_birth = new \DateTime($date);
return $date_of_birth->format($format);
}
public function formatDate($date) {return date('d/m/Y', strtotime($date));}
Related
I'm very new using Laravel and I have a problem with a column that I created in the migrations. I have a column that is type date. My problem is that I don't know how can I manage it, how do I have to store some data for a form, etc.
I was asking if someone could tell me, in a method of a controller with the parameter Resquest $r, how can a convert the string from that form in a variable of date. I've been searching and I can't find anything that could help me. Thanks in advance!
Edit:
This is one method of my controller:
public function addObjectives(Request $r,$id){
$gobjectives = Mgobjective::all();
$sobjectives = Msobjective::all();
$mprogram = Mprogram::find($id);
$a = Area::find($mprogram->marea_id);
if($a==null){
$area = new Area();
$area->id = $mprogram->marea_id;
$area->saveArea();
}
$p = Program::find($id);
if($p==null){
$program = new Program();
$program->id = $id;
$program->initialDate= "";
$program->finalDate= "";
$program->frequency= "";
$program->area_id = $mprogram->marea_id;
$program->saveProgram();
}
}
In initialDate y finalDate de program, I have an empty string because before it was a string. I would like to put there a date that doesn't have to be the actual date.
Use date() function like:
$date = date('Y-m-d', strtotime('your date'));
Explanation: Y-m-d is the default date format for mysql date type. So what ever formated date you have, convert it into Y-m-d.
<?php
namespace App\Http\Controllers;
class YourController extends Controller
{
public function yourMethod(Request $request)
{
// If you use a HTML date element, you "should" receive the data in yyyy-mm-dd
// format which can be understood by DateTime's constructor
$date = new \DateTime($request->input('date'));
// If you use a different input or expect a date in a different format you can
// use the createFromFormat method, where your first parameter is the date
// format you expect and the second is the date input.
$date = \DateTime::createFromFormat('d/m/Y', $request->input('date'));
// Once your date has been loaded into a DateTime object, you can use the
// format method to output the date in any format you like. MySQL and other
// databases tend to expect and store dates in yyyy-mm-dd format.
echo $date->format('Y-m-d');
}
}
Try using Carbon
Carbon::createFromDate($year, $month, $day, $tz);
or
Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00
Take a look at the documentation
I would like to understand how to change different date formats into a single format using a php function. After trying in every way, i wasn't able to solve this puzzle. I always just used the following code in my custom function.php smoothly:
/* Change date format for coupon */
function change_date_format($x) {
$date = DateTime::createFromFormat('j-M-Y', $x);
$x = $date->format('Y-m-d');
return $x;
}
In this way i can convert the format 'j-M-Y' in the format 'Y-m-d'. The problem is that now i need to convert not only the date format 'j-M-Y', but also other formats (for example, i've to convert date format 'j-M-Y' and date format 'Y-m-d\TH:i:sP' in date format 'Y-m-d'. I tried to combine different logic functions but system gives me error.
Thanks to all of you who try to help me...
The DateTime class is pretty good at parsing different formats without createFromFormat(). If the formats you have are supported (Supported Date and Time Formats) then just let it create based on the in-built logic. If $x = '2016-06-30T23:59:59+02:00' then the DateTime class handles this just fine:
function change_date_format($x) {
$date = new DateTime($x);
return $date->format('Y-m-d');
}
Add an input parameter to your function called: $inputFormat and use this instead 'j-M-Y', so you should specify always the input format. You can specify a default format for input.
/**
* Return with a normal format of any date by given format
* Default format is j-M-Y if no input format given
*
* #param string $dateString
* #param string $inputFormat
* #return string
*/
function change_date_format($dateString, $inputFormat = 'j-M-Y') {
$date = DateTime::createFromFormat($inputFormat, $dateString);
return $date->format('Y-m-d');
}
echo change_date_format('23-05-2016', 'd-m-Y');
echo change_date_format('05/23/2016', 'm/d/Y');
You can use an additional parameter as follows:
/*Change date format for coupon*/
function change_date_format($x, $dateFormat) {
$date = DateTime::createFromFormat($dateFormat, $x);
$x = $date->format('Y-m-d');
return $x;
}
I have date in format "m-Y-d". How can i modify this date to format "Y-d-m"? For this the best should be function where i can add old format and new format, but how can i make it?
For example i have
$date = '01-2013-13'; // "m-Y-d"
i would like receive:
$newDate = '2013-13-01'; // "Y-d-m"
See DateTime::createFromFormat() and DateTime::format()
$date = DateTime::createFromFormat('m-Y-d', '01-2013-13');
echo $date->format('Y-m-d');
PHP cannot parse the format using strtotime(). You'll have to do something like this:
function reformat($old_date)
{
$parts = explode('-', $old_date);
return $parts[1].'-'.$parts[2].'-'.$parts[0];
}
And then call it using:
$new_format = reformat($date);
Alternatively, you can use the DateTime::createFromFormat():
function reformat($old_date)
{
$new_date = DateTime::createFromFormat('m-Y-d', $old_date);
return $new_date->format('Y-m-d');
}
You can use the DateTime class, for example:
$date = new DateTime('01-2013-13');
Then use the format() method, like:
$date->format('Y-m-d');
More info:
http://www.php.net/manual/en/datetime.format.php
To create you need to use:
$newDate = date('Y-d-m');
You can find more information here!
I've some data which formatted like: 04.09.1953
I want to convert this format to: 1953-09-04
Is there any way or php function to do this?
just use strtotime() to get a timestamp and then date() to convert that timestamp to the format you need:
$timestamp = strtotime("04.09.1953");
echo date("Y-m-d", $timestamp);
EDIT:
If you're having some "exotic" format as input, you might need to use explode(), list() and mktime() to build the timestamp on your own:
list($y,$m,$d) = explode(".","04.09.1953");
$timestamp = mktime(0,0,0,$m,$d,$y);
echo date("Y-m-d", $timestamp);
Have you tried strtotime() ? It might work, else you'll need to do manual conversion using substrings or explodes.
http://php.net/strtotime
http://php.net/substring
http://php.net/explode
If all you are interested in is converting from one "string" format to another you can use a regEx:
$DMY = '04.09.1953';
$YMD = preg_replace('/(\d\d).(\d\d).(\d{4,4})/', '$3-$2-$1', $DMY);
http://www.handyphp.com/index.php/PHP-Resources/Handy-PHP-Functions/reformat_date.html
function reformat_date($date, $format){
$output = date($format, strtotime($date));
return $output;
}
Does anyone know of any good functions for validating a date via input text box in codeigniter like this: dd/mm/yyyy? Thanks
You're probably looking for the strptime function. For the specified format you can use it like this:
$result = strptime($input, '%d/%m/%Y');
$resultwill be either false if the input is invalid or on array with the date.
Or if you're on Windows or have PHP >= 5.3, consider using date_parse_from_format.
Use strtotime function.
$date = strtotime($input);
returns false if invalid, or -1 for php4.
you could also use a custom callback function
<?php
function valid_date($date)
{
$date_format = 'd-m-Y'; /* use dashes - dd/mm/yyyy */
$date = trim($date);
/* UK dates and strtotime() don't work with slashes,
so just do a quick replace */
$date = str_replace('/', '-', $date);
$time = strtotime($date);
$is_valid = date($date_format, $time) == $date;
if($is_valid)
{
return true;
}
/* not a valid date..return false */
return false;
}
?>
your validation rule looks like:
$rules['your_date_field'] = "callback_valid_date";
dont know about codeigniter but there's some alike already in php already
checkdate();