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
Related
i'm very new to this Forum. I'm working on my own website and got a problem.
Because i'm very new to coding and not very skilled with php i can't find a solution for this little problem.
I would like to formate my date from the Database to a "friendlydate"
e.g. Date from Database: 2016-06-08 00:00:00
my wish-date: 08.06.2016
Here is my Code from the viewmanager, where i want do define the
"friendlydate"
// assign values to view object
$viewBlog->id = $value->id;
$viewBlog->bild = $value->bild;
$viewBlog->date = $value->date;
$viewBlog->author = $value->author;
$viewBlog->title = $value->title;
$viewBlog->text = $value->text;
$viewBlog->category_id = $value->category_id;
if (strlen($value->text) > 280) {$viewBlog->shorttext = substr($value->text,0,280)."...";} else {$viewBlog->shorttext = $value->text;}
***$viewBlog->friendlydate = here is my problem;***
$viewBlog->objCategory = $this->getViewCategory($value->category_id);
You can parse your original date in to a DateTime object which will then allow you to format the date however you like. For instance:
$date = new DateTime($value->wish-date);
$viewBlog->friendlydate = $date->format('Y-m-d H:i:s');
In this case, friendlydate would be 2016-06-08 00:00:00. To see how to specify what format you like see the documentation.
Assuming $viewBlog->friendlydate is your date variable,
$viewBlog->friendlydate = date("m.d.Y");
where m is numeric representation of a month, with leading zeros, n is numeric representation of a month without leading zeros and Y is a full numeric representation of a year output as 4 digits.
Using string functions:
$parts = explode('-', substr('2016-06-08 00:00:00', 0, 10));
$date = $parts[2].'.'.$parts[1].'.'.$parts[0];
This will convert the string as you have described. You may also want to look into PHP date functions.
You will just need to reformat your date. I am really fond of the DateTime method in php.
// Get the current date with its format
$date = DateTime::createFromFormat('Y-m-d H:i:s', $value->date);
// Convert it to a new format
$viewBlog->date = $date->format('d.m.Y');
In the resource below you can find information about different formats in which you can output your date.
Resources
DateTime - Manual
I have a date 01/31/2014, and I need to add a year to it and make it 01/31/2015. I am using
$xdate1 = 01/31/2014;
$xpire = strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year");
But it is returning 31474800.
Waaaay too complicated. You're doing multiple date<->string conversions, when
php > $x = strtotime('01/31/2014 +1 year');
php > echo date('m/d/Y', $x);
01/31/2015
would do the trick.
Another way is below:
<?php
$date = new DateTime('2014-01-31');
$date->add(new DateInterval('P01Y'));
echo $date->getTimestamp();
?>
There are 2 mistakes here.
You are missing the quote sign " when assigning to $xdate1. It should be
$xdate1 = "01/31/2014";
And the second, to get "01/31/2015", use the date function. strtotime returns a timestamp, not a date format. Therefore, use
$xpire = date("m/d/Y", strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year"));
May I introduce a simple API extension for DateTime with PHP 5.3+:
$xdate1 = Carbon::createFromFormat('m/d/Y', '01/31/2014');
$xpire = $xdate1->addYear(1);
First make $xdate1 as string value like
$xdate1 = '01/31/2014';
then apply date function at it like bellow
$xpire = date('m/d/Y', strtotime($xdate1.' +1 year')); // 01/31/2015
So I have the date like this in wordpress, I get it from a custom metabox where is stored like this, 23/02/2012, now how can I set wp_locale in WP or something like that, and I need to convert the date to: Monday 23 February 2012, but I need also to set the language, thats why I need that wp_locale because that output will be in Dutch.Thank you
The date format can be converted with the code below as a guide.
date('l j F Y', strtotime(str_replace('/', '-', '23/02/2012')))
The str_replace is necessary because with / PHP assumes m/d/y American date format, not the European d/m/y.
As for doing this in Wordpress with i18n support, you might consider http://codex.wordpress.org/Function_Reference/date_i18n
date_i18n('l j F Y', strtotime(str_replace('/', '-', '23/02/2012')))
I guess there are other ways to do this, but I'd do it as follows:
Upon 'save_post':
$date = explode ('/',$_POST["date_field"]);
$date = $date[1].'/'.$date[0].'/'.$date[2]; // dd/mm/yyyy to mm/dd/yyyy
$s = strtotime($date); /* UNIX TIMESTAMP */
and then store the Unix timestamp in the database.
Upon 'amdin_init' you'll need to do the exact opposite to load the date in the right format in the metabox:
global $post;
$custom = get_post_custom($post->ID);
if ($custom["date_field"][0]) {
$d = date("d/m/Y",$custom["date_field"][0]); // convert unix timestamp
} else {
$d = "";
}
And in your template file, use php functions setlocale and strftime to display the date:
$custom = get_post_custom($post->ID);
$d = $custom["date_field"][0];
setlocale(LC_TIME, 'nl_NL');
$s = strftime('%#d %B %Y',$s);
This should output something like 13 January 2012.
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 a date/time string like this: 180510_112440 in this format ddmmyy_hhmmss
I need a snippet for having a string formatted like this way: 2010-05-18 11:24:40
Thanks for help.
another possible answer is the common use of strptime to parse your date and the mktime function:
<?php
$orig_date = "180510_112440";
// Parse our date in order to retrieve in an array date's day, month, etc.
$parsed_date = strptime($orig_date, "%d%m%y_%H%M%S");
// Make a unix timestamp of this parsed date:
$nice_date = mktime($parsed_date['tm_hour'],
$parsed_date['tm_min'],
$parsed_date['tm_sec'],
$parsed_date['tm_mon'] + 1,
$parsed_date['tm_mday'],
$parsed_date['tm_year'] + 1900);
// Verify the conversion:
echo $orig_date . "\n";
echo date('d/m/y H:i:s', $nice_date);
$inDate = '180510_112440';
$date = strtotime('20'.substr($inDate,4,2).'-'.
substr($inDate,2,2).'-'.
substr($inDate,0,2).' '.
substr($inDate,7,2).':'.
substr($inDate,9,2).':'.
substr($inDate,11,2));
echo date('d-M-Y H:i:s',$date);
Assumes date will always be in exactly the same format, and always 21st century
list($d,$m,$y,$h,$i,$s)=sscanf("180510_112440","%2c%2c%2c_%2c%2c%2c");
echo "20$y-$m-$d $h:$i:$s";