Show proper date before 1970 - php

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

Related

PHP Date is showing as AM instead of PM

Creating json with date
$cart[] = array('title' => $json_decoded->event, 'start' => (Carbon::parse(date('Y-m-d h:i:s', strtotime("$json_decoded->date $json_decoded->time"))))->format('c'));
Passing this json to Full Calendar
[{"title":"Golf","start":"2022-06-14T04:00:00+12:00"}]
Database shows 16:00
Comes out as 4am

Is there a fast way to convert date to spanish?

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.

Add 1 month to part string date

wasnt sure how to best describe this. I make an API call and one of the elements I get back is like the following
0 => array:3 [▼
"customId" => "12345"
"customName" => "Month"
"customValue" => "June 16"
]
As you can see the customValue is June 16, meaning June 2016. Is there any way I can add 1 month onto this, so to make it July 16? I know I could simply replace Jun with July using something like str_replace, but I do not want to change the expression month on month. What I am really looking for is to have the system understand that this is Jun 16 and 1 month needs adding onto it.
Would this be possible?
Thanks
To change the value of $monthAndDay:
$monthAndDay = "June 16";
$monthAndDay = date("F j", strtotime($monthAndDay." + 1 month"));
echo $monthAndDay; //outputs July 16
Within an array:
$arr = [
"customId" => "12345"
"customName" => "Month"
"customValue" => "June 16"
];
$arr["customValue"] = date("F j", strtotime($arr["customValue"]." + 1 month"));
http://php.net/manual/en/function.jdmonthname.php
Look at this.
It could be a way to do it with this command!
Hope I could help,
Skayo

Show date with custom month name, php

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');
?>

With PHP, how to retrieve items that match created month and year

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())]);

Categories