Im new to php and not sure how to operate with the string or date. I have custom month names, stored in array:
$months = ['1' => 'my Jan', '2'=>'my Feb', '3'=>'my Mar', '4'=>'my Apr', //etc.];
I'd like to show the date with these month names, currently I show:
date_format(new DateTime($route->start_date), 'd F Y')
which gives 01 January 2016
I need to get 01 my Jan 2016.
Thanks
hope this will help.
<?php
$months = ['1' => 'my Jan', '2'=>'my Feb', '3'=>'my Mar', '4'=>'my Apr'];
$date = new DateTime($route->start_date);
echo '<br>'.$date->format('d').' '.$months[$date->format('n')].' '.$date->format('Y');
?>
Related
I am inserting selected DOB into database in "yyyy-mm-dd" formate using jquery date-picker. But when I am selecting DOB before 1970 then it gives me wrong DOB. eg. we select "August 19, 1949" then it show "August 19, 2049" in future year that is wrong. We are showing DOB in front-end using below mentioned code :
echo $newDate = date("M dS, Y", strtotime($BirthDetails['date']));
So please help me !!!
I'm using cakephp 3.0
When I print $BirthDetails['date'], it gives me
Cake\I18n\FrozenDate Object
(
[time] => 2011-08-19T00:00:00+00:00
[timezone] => UTC
[fixedNowTime] =>
)
Try this, use createFromFormat
// pass your date format
$date = DateTime::createFromFormat('d M Y','17 Jan 1949');
echo $date->format('Y-m-d');
DEMO
After a long period of searching, I found out my answer. We just need to do some changes in config/app.php file under the 'App' => []
replace
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
to
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'pl_PL'),
this is proper working in my scenario
With this:
date( 'd F Y', strtotime( $row["datestart"] ) )
I get this:
08 July 2016
But I need to get this:
08 Julio 2016
Julio is July in spanish.
I have added this to the top of the php page:
setlocale(LC_TIME, 'es_ES');
but it doesn't work.
So what can I do ?
This worked for me:
setlocale(LC_TIME, 'es_ES', 'Spanish_Spain', 'Spanish');
$date = str_replace("/","-","08/07/2016");
echo strftime('%d %B %Y',strtotime($date)); // 08 julio 2016
setlocale is the key ingredient here.
update: PHP 8.x
$format = new IntlDateFormatter('es_ES', IntlDateFormatter::SHORT, IntlDateFormatter::NONE, NULL, NULL, 'dd MMMM y');
echo $format->format(new DateTime('now', new DateTimeZone('UTC')));
Use format to output the DateTime to the locale you specify in IntlDateFormatter (DateTimeZone is optional).
Another variant you may use:
Install intl extension for php (link).
Enable it in your php.ini file and then you will be able to check it is working with the following sample:
$f = new IntlDateFormatter('es_ES', null, null, null, null, null, 'dd MMMM y');
print($f->format(new DateTime('2016-07-08'));
An expected output will be the following:
08 julio 2016
You could make an associative array.
Set the keys to the months in English and the values to the corresponding months in Spanish.
It would look something like this...
$months = array(
'january' => 'enero',
'february' => 'febrero',
'march' => 'marzo',
'april' => 'abril',
'may' => 'mayo',
'june' => 'junio',
'july' -> 'julio',
'august' => 'agosto',
'september' => 'septiembre',
'october' => 'octubre',
'november' => 'noviembre',
'december' => 'diciembre'
);
Then you could reference the months like this...
$enMonth = "july"; //This is the month in English that you will match to the corresponding month in Spanish.
$esMonth = $months[$enMonth]; //You are returning the value (which is Spanish) of the key (which is English), giving you the month in Spanish.
You could probably also use Google Translate's API, but it seems like too much for something that can be done with a simple array.
Here is Google Translate's API if you are interested in translating other words, or a larger array of words.
I've got bunch of birthdays which are stored in format DDMMMYY. I need to convert those to date values, so i can store those in database.
Is there any easy way of telling strtotime function that date must be in the past?
<?php
$datestring = '22SEP41';
echo date('Y-m-d',strtotime($datestring)); //outputs 2041-09-22, should be 1941-09-22
?>
<?php
$datestring = '22SEP41';
$matches = [];
preg_match('/([0-9]{2})([A-Z]{3})([0-9]{2})/', $datestring, $matches);
$prefix = ($matches[3] <= date('y') ? '20' : '19');
$matches[3] = "{$prefix}{$matches[3]}";
$ts = strtotime("{$matches[1]} {$matches[2]} {$matches[3]}");
// date ('Y-m-d', $ts) == 1941-09-22
This assumes that 22SEP06 should be interpreted as 2006 rather than 1906 - basically it gives the output a range of 1917 -> 2016.
This method create a date of past century only if standard evaluated date is after today:
$date = date_create( $datestring );
if( $date->diff( date_create() )->invert )
{
$date->modify( '-100 years' );
}
echo $date->format( 'Y-m-d' );
For
$datestring = '22SEP41';
the output is:
1941-09-22
For
$datestring = '22SEP01';
the output is:
2001-09-22
eval.in demo
Basically, we create a DateTime based on given string, then we calculate difference with current day; if the difference is negative (->invert), we subtract 1 century from the date.
You can personalize the condition using ->format('%R%Y') instead of ->invert. In this example:
if( $date->diff( date_create() )->format('%R%Y') < 10 )
Dates from 00 through 05 as evaluated as 2000-2005.
You could try something like:
$ds = '22SEP41';
$day = $ds[0].$ds[1];
// Getting the month.
$mon = array(
'JAN' => 1,
'FEB' => 2,
'MAR' => 3,
'APR' => 4,
'MAY' => 5,
'JUN' => 6,
'JUL' => 7,
'AUG' => 8,
'SEP' => 9,
'OCT' => 10,
'NOV' => 11,
'DEC' => 12
);
$mont = $ds[2].$ds[3].$ds[4];
$month = $mon[$mont]; // Gets the month real.
$year = '19'.$ds[5].$ds[6];
$final = $day.'-'.$month.'-'.$year;
I tested it on my local machine and it worked. Hope it works and is what you're looking for :)
I'm using this code to retrieve news items from my MongoDB:
foreach (collection("news")->find(["created"=>$time]) as $news)
Now I would like to find only those news articles with "created" unix timestamp that matches specific month of the specific year, like April 2014.
Any help would be appreciated.
Cheers.
EDIT: Thank you all for your effort, and yes, it's unix timestamp.
How did you create the Unix Timestamp? How is it represented in the document?
Are you using the MongoDate type, or are you using an integer?
If you are using the MongoDate type then you need to construct MongoDate objects and use them for your $gte and $lt conditions
$article = array(
"title" => "My first article",
"content" => "This is good news",
"published" => new MongoDate(),
"author" => "Random guy on the internet",
);
$collection->insert($article);
$start = new MongoDate(strtotime("yesterday"));
$end = new MongoDate(strtotime("tomorrow"));
$cursor = $collection->find(array("ts" => array('$gt' => $start, '$lte' => $end)));
foreach($cursor as $article) {
var_dump($article);
}
See http://www.php.net/manual/en/class.mongodate.php for more info
you'll have build your own query:
SELECT * FROM news WHERE created >= $start_date AND created <= $end_date
where:
assuming created is unix timestamp, otherwise you should skip strtotime function
// first day of april 2014
$start_date = strtotime(date('2014-04-01'));
// last day of april 2014
$end_date = strtotime(date('2014-04-t'));
You could use DateTime::createFromFormat to create your timestamps.
$begin = DateTime::createFromFormat('!Y-m', '2014-04');
$end = clone $begin;
$end->modify('next month');
echo $begin->format('Y-m-d H:i:s'). PHP_EOL; // 2014-04-01 00:00:00
echo $end->format('Y-m-d H:i:s'); // 2014-05-01 00:00:00
And build more complex condition.
collection("news")
->find(["created" => array(
'$gte' => $begin->getTimestamp(),
'$lt' => $end->getTimestamp())]);
Hi #ll and greetings from Germany,
Perhaps I am too blind to see, but I am struggling with a localisation problem. I hope that someone has a solution for that.
In a function I have a german date string, assume it's today:
$datestring = '3. März 2014'
Second, i have the date format used in WP
$date_format = get_option( 'date_format' );
What I am trying to achive is to get a valid unix timestamp from $datestring.
I tried several different approaches like strtotime, date_parse_from_format, etc., I even tried to set the locale environment via setlocale. Of course it would be easy to parse the string against an array with the month names and get an english datestring, but I want to have this ready for all languages.
Any Idea how to get this done? Help is really appreciated.
You need to use mktime($datestring)
and set the $datestring variable in accordance to:
http://pl1.php.net/manual/de/function.mktime.php
in wordpress:
mktime(get_the_date("H"), get_the_date("i"), get_the_date("s"), get_the_date("n"), get_the_date("j"), get_the_date("Y"));
if you need to convert exact format shown above:
$datestring = '3. März 2014';
$dateelements = explode(" ", $datestring);
$day = rtrim($dateelements[0], ".");
$germanMonths = array(1 => "Januar", 2 => "Februar", 3 => "März", 4 => "April", 5 => "Mai", 6 => "Juni", 7 => "Juli", 8 => "August", 9 => "September", 10 => "Oktober " , 11 => "November", 12 => "Dezember");
$month = array_search($dateelements[1], $germanMonths);
$year = $dateelements[2];
$unixtimestamp = mktime(0,0,0,$month,$day,$year);
and if you have a date format in wp option, and $datestring is in that format:
$dateformat = get_option('date_format');
echo $dateformat;
$date = date_create_from_format($dateformat, $datestring);
$new_format = date_format($date, 'm,d,Y');
$unixtimestamp = mktime(0,0,0,$new_format);
echo $unixtimestamp;