PHP Date is showing as AM instead of PM - php

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

Related

Correct epoch to DateTime conversion

Whit this code:
$epoch= '1609455600';
$date = new DateTime( '#'.$epoch);
echo $date-> format( 'Y-m-d');
I see this result 2020-12-31. The server timezone is reported as Europe/Zurich (with date_default_timezone_get). But in this time zone that date should be 2021-1-1.
What is going on here?
In addition to the comment from #tuckbros. The output with var_export shows that the DateTime object has the time zone 00:00 (UTC).
date_default_timezone_set('Europe/Zurich');
$epoch= '1609455600';
$date = new DateTime( '#'.$epoch);
var_export($date);
/*
DateTime::__set_state(array(
'date' => '2020-12-31 23:00:00.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
))
*/
The clean way to get the local time is to transfer the object to the desired time zone (and not to add any offset times).
$date->setTimeZone(new DateTimeZone(date_default_timezone_get()));
var_export($date);
/*
DateTime::__set_state(array(
'date' => '2021-01-01 00:00:00.000000',
'timezone_type' => 3,
'timezone' => 'Europe/Zurich',
))
*/
You can now continue to work with the DateTime object, since it has the correct time zone in addition to the correct local time.

Grouping data to get reports by intervals

I posted a question some time ago on representing data per date horizontally on a datatable.
See here: datatables dates at the top and data cells going from left to right
With the help of that thread I was able to get the data to display how I wanted it. With the dates showing at the top, the service provided on the left and all data associated with any date between the 2 date paramters inside the main body. (If there is no data in a particular date then the < td > will display 0. See here:
http://www.phpwin.org/s/ewbAS6
After manipulating this code further I made the dates of the search dynamic by proving a form with a start date and an end date, and a dropdown with the options of:
Daily
Weekly
Monthly
Quaterly
Yearly
this allows the interval of dates at the top to become dynamic. Of course all this is doing is changing the value of the 2nd parameter inside the date while loop.
WHILE (strtotime($date) <= strtotime($end_date)) {
echo '<th>' . $date . '</th>';
$date = date('Y-m-d', strtotime($date . ' +1day'));
}
with the parameter set at Weekly, the value of +1day becomes +1week, at Monthly; the value becomes +1month and so on.
MY ISSUE:
When the interval is set to daily, the dates with their corresponding attendance counts are displayed correctly but once you try to increase the interval to +1week and above the data does not round up to the week shown. Check this:
[LINK1]Per day: http://www.phpwin.org/s/ewbAS6
[LINK2]Per month: http://www.phpwin.org/s/xRo3I6
Looking at the array (modified on the LINK2)
$result[] = array('Service Name' => 'Health', 'date' => '2017-04-04', 'Attendance' => 5);
$result[] = array('Service Name' => 'Payroll', 'date' => '2017-04-16', 'Attendance' => 5);
$result[] = array('Service Name' => 'Saturday Youth Meeting', 'date' => '2017-04-03', 'Attendance' => 1);
$result[] = array('Service Name' => 'Saturday Youth Meeting', 'date' => '2017-05-03', 'Attendance' => 3);
$result[] = array('Service Name' => 'Payroll', 'date' => '2017-05-03', 'Attendance' => 2);
$result[] = array('Service Name' => 'Payroll', 'date' => '2017-04-11', 'Attendance' => 3);
$result[] = array('Service Name' => 'Payroll', 'date' => '2018-04-03', 'Attendance' => 10);
You can see in the array that there are multiple attendance entries in April, totaling 14 Attendances in that month however during LINK2 where the interval is increased to a month instead of showing 14 for April (which would be the sum of all the dates in that particular month) it shows the value 1.
My live version takes the array from a database so I used the YEAR(), MONTH(), WEEK() and DAY() function on the date and used group by. The query executes how I want it but having issues working on the PHP end.

Show proper date before 1970

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

How to display only date in symfony sfWidgetFormDate but it should pass month and day as default '1'?

I have to display date alone in sfWidgetFormDate but I want date and month to be passed in hidden.
I have given as:
$this->widgetSchema['fellowship_admission_date'] = new sfWidgetFormFilterDate(array(
'from_date' => new sfWidgetFormDate(array('format' => '%year%')),
'to_date' => new sfWidgetFormDate(array('format' => '%year%')),
'with_empty' => false
))
It displaying only year but I want to pass month and day in 'from_date' and 'to_date' default as '1' as hidden.
Why don't you display the entire date, and hide day and month with jQuery?!
It could be a good and fast solutions...

Categories