Hi I am trying to fix an old piece of code created by another developer. Spent a while fixing it to work with mysqli. Anyway the last thing on my list is to fix the dates error.
there is no actual error but if you submit the form with a valid date on it always shows up in the table as 01-01-1970
Below is the piece of code which is formatting the date. The date enters this in the format DD-MM-YY.
$arr[$i] = strftime("%Y-%m-%d", strtotime($date) );
The result so far is always 01-01-1970
here are some of y attempts at fixing it all with the same output:
$arr[$i] = $date;
$arr[$i] = strtotime($date);
and many variations of the strftime settings!
have you considered using date()?
string date ( string $format [, int $timestamp = time() ] )
http://php.net/manual/en/function.date.php
so...
$arr[$i] = strftime("%Y-%m-%d", strtotime($date) );
becomes
$arr[$i] = date("Y-m-d", strtotime($date) );
Try using DateTime objects and createFromFormat, so you can explicitly define the format of your $date value
$dt = DateTime::createFromFormat('d-m-y',$date);
$arr[$i] = $dt->format("Y-m-d");
Related
My intention is to convert the following date
20/04/17 13:27:5
to this
20-04-2017 13:27:05
I tried the typical date format functions of php and also Carbon...
things like
$newDate= Carbon::createFromFormat('d/m/y H:m:s', $originalDate);
in this case
var_dump($newDate->toDateTimeString()) would bring 2019-03-20 13:00:55 which is not what I expect.
So I was not lucky....is there a way to do this in a straight forward manner?
I think this should work.
$date = "20/04/17 13:27:5";
$sec = substr($date, strrpos($date, ":") + 1);
$sec = substr("0{$sec}", -2);
$new = substr($date, 0, strrpos($date, ":") + 1) . $sec;
$newDate = Carbon::createFromFormat('d/m/y H:i:s', $new);
I changed the format since you were using m twice for "minutes" and "month". It is correct for the month, but not for the minutes. Instead use i for minutes with leading zeroes.
$sec Is what I used to get the second from the string. This gets the last position of : and will take everything after it. This assumes that you do not change the format of the string.
substr("0{$sec}", -2) Adds a zero to the current second and extracts the last two characters. That means that 50 becomes 050 and then the last two characters are 50 so we end up without the padding, but 5 becomes 05 and the last two characters are the only characters.
$new concatenates the start of the date string and the new second with the zero padding.
$newDate is your original string with the format changed.
There is issue with seconds. There must be 05 not only 5
<?php
$original_date = "20/04/17 13:27:5";
$date_explode = explode(":", $original_date);
$date_explode[2] = str_pad($date_explode[2],2,"0",STR_PAD_LEFT);
$original_date = implode($date_explode,":");
$date = DateTime::createFromFormat('d/m/y H:i:s', $original_date);
echo date_format($date,"d-m-Y H:i:s");
?>
This is a working conversion routine that creates the ISO format you are looking for. But as already mentioned you need to "fix" the strange way the seconds are specified in the original example you provide. You will have to use string functions if that really is the format you receive. Better would be to fix the code that creates such broken formats.
<?php
$input = '20/04/17 13:27:05';
$date = DateTime::createFromFormat('d/m/y H:i:s', $input);
var_dump($date->format('d-m-Y H:i:s'));
The output obviously is:
string(19) "20-04-2017 13:27:05"
Isn't it like this?
$newDate = Carbon::createFromFormat('d/m/y H:i:s', $originalDate);
In my site, I have a bootstrap datepicker which allows user to pick date in format of MM/DD/YYYY (e.g: 05/12/2014). Then when this data is submitted, I used the following PHP code to convert it into Datetime type, then insert into start_date (DATETIME datatype) column in MySQL .
$start_date = date('Y-m-d', $_POST['start_date']);
the insert query in PHP does nothing with reformatting the date. It just simply insert into corresponding column.
However, instead of inserting '2014-05-12', the value inserted into database is '1970-01-01'. That's so weird to me. Can anybody tell me what's wrong here. Is this that I used incorrect PHP function or incorrect timezone setting or ...
Just do this:
$start_date = date('Y-m-d', strtotime($_POST['start_date']));
You could also use strtotime() on your $_POST.
$start_date = date('Y-m-d', strtotime('05/12/2014'));
try to use
$date = str_replace('/', '-', $_POST['start_date']);
$start_date = date('Y-m-d', strtotime($date));
For more :- Converting between illogically formatted dates (changing /slash/ to -dash- )
Hi I know this question is a bit trivial. I googled it but could not find the exact problem anywhere.
I have a string which contains a application filling date how can I convert this string into date
$appln_filling_date = '20020315';
The type of appln_filling_date is string I want to convert its type to date and want the data remain the same. The field in the database has a type date.
EDIT
This is what I am trying to do
$appln_filling_date = strtotime($appln_data['bibliographic-data']['application-reference']['document-id']['1']['date']['$']);
$appln_filling_date = date('Y-m-d', $appln_filling_date);
If your date is in some standard date format use this example:
$d = new DateTime('20020315');
echo $d->format('Y-m-d');
# or [if you do not have DateTime, which is in php >= 5.2.0]
$t = strtotime('20020315');
echo date('Y-m-d', $t);
If your date is not in some standard format use this example:
$d = DateTime::createFromFormat('d . Y - m', '15 . 2002 - 03');
echo $d->format('Y-m-d');
$date = DateTime::createFromFormat('Ymd', $appln_filling_date);
This worked for me like a charm
$appln_filling_date = $appln_data['bibliographic-data']['application-reference']['document-id']['1']['date']['$'];
$appln_filling_date = date_create(date('Y-m-d', $appln_filling_date));
Thanks for the down voting
I'm having date 20/12/2001 in this formate . i need to convert in following format 2001/12/20 using php .
$var = explode('/',$date);
$var = array_reverse($var);
$final = implode('/',$var);
Your safest bet
<?php
$input = '20/12/2001';
list($day, $month, $year) = explode('/',$input);
$output= "$year/$month/$day";
echo $output."\n";
Add validation as needed/desired. You input date isn't a known valid date format, so strToTime won't work.
Alternately, you could use mktime to create a date once you had the day, month, and year, and then use date to format it.
If you're getting the date string from somewhere else (as opposed to generating it yourself) and need to reformat it:
$date = '20/12/2001';
preg_replace('!(\d+)/(\d+)/(\d+)!', '$3/$2/$1', $date);
If you need the date for other purposes and are running PHP >= 5.3.0:
$when = DateTime::createFromFormat('d/m/Y', $date);
$when->format('Y/m/d');
// $when can be used for all sorts of things
You will need to manually parse it.
Split/explode text on "/".
Check you have three elements.
Do other basic checks that you have day in [0], month in [1] and year in [2] (that mostly means checking they're numbers and int he correct range)
Put them together again.
$today = date("Y/m/d");
I believe that should work... Someone correct me if I am wrong.
You can use sscanf in order to parse and reorder the parts of the date:
$theDate = '20/12/2001';
$newDate = join(sscanf($theDate, '%3$2s/%2$2s/%1$4s'), '/');
assert($newDate == '2001/12/20');
Or, if you are using PHP 5.3, you can use the DateTime object to do the converting:
$theDate = '20/12/2001';
$date = DateTime::createFromFormat('d/m/Y', $theDate);
$newDate = $date->format('Y/m/d');
assert($newDate == '2001/12/20');
$date = Date::CreateFromFormat('20/12/2001', 'd/m/Y');
$newdate = $date->format('Y/m/d');
I have problem with date function in php. If I supply string in format "d.m.y" for example "01.01.01" it gets rendered as todays date which means that php gets confused.
I found:
Note:
The "Day, month and two digit year, with dots or tabs" format (dd [.\t] mm "." yy)
only works for the year values 61 (inclusive) to 99
(inclusive) - outside those years the time format "HH [.:] MM [.:] SS" has
precedence.
on: php.net site
How to override this behavior?
I know of date_create_from_format function which would work fine if I knew input will always be in format "d.m.y", but it won't.
UPDATE 1:
Code
$date = new DateTime('01.01.01');
echo $date->format('Y-m-d');
outputs 2010-10-19 and I wanted 2001-01-01.
To format a date other than now, use the second parameter. For example:
echo date("d.m.y", 1255982665);
echoes 19.10.09
Just read the documentation! PHP's site is excellent
It seems like you want to reformat a date?
mktime() gives unix timestamp from component pieces
date() gives string from unixtimestamp (or implied now)
getdate() gives assoc array from unix timestamp
I think you want -
$arr = explode($dateIn, ':'); //get array [day, month, year]
$timestamp = mktime(0,0,0, $arr[0], $arr[1], $arr[2]) //unix time stamp, a long integer representing time
date(DESIREDFORMAT, $timestamp);
check out the output formats here - http://us2.php.net/manual/en/function.date.php
function getDateFromString( $str )
{
$date = DateTime::createFromFormat( 'd.m.y', $str );
if ( $date !== false )
return $date->getTimestamp();
// you can try other common formats here
// ...
// otherwise just parse whatever there is
return strtotime( $str );
}
echo date( 'd.m.Y H:i', getDateFromString( '01.01.01' ) ); // 01.01.2001 20:14
Edit
To adjust it a bit more to get your exact output:
function getDateTimeFromString( $str )
{
$date = DateTime::createFromFormat( 'd.m.y', $str );
if ( $date !== false )
return $date;
// you can try other common formats here
// ...
// otherwise just parse whatever there is
return new DateTime( $str );
}
$date = getDateTimeFromString( '01.01.01' );
echo $date->format( 'Y-m-d' ); // 2001-01-01