Zend_Date: wrong results on DST change day - zend-framework

"25 Mar 2012" was the date when time was changed from 02:00am to 03:00am in the Czech Republic. On that date, one functionality on my website stopped working correctly and a customer complained, etc. After digging for a few hours, I figured out that on that day Zend_Date behaved strangely:
#!/usr/bin/env php
<?php
include 'Zend/Date.php';
date_default_timezone_set('Europe/Prague');
shell_exec('sudo date --set="25 Mar 2012 12:00:00"');
$date = new Zend_Date();
$date->set('00:01:00', Zend_Date::TIMES);
$startDate = $date->get(Zend_Date::TIMESTAMP);
echo 'start date: ' . date("j.n.Y H:i", $startDate) . PHP_EOL;
$date->set('23:59:00', Zend_Date::TIMES);
$endDate = $date->get(Zend_Date::TIMESTAMP);
echo 'end date: ' . date("j.n.Y H:i", $endDate) . PHP_EOL;
This outputs:
start date: 24.3.2012 23:01
end date: 24.3.2012 23:59
which is off by day.
If I change the date to "26 Mar 2012 12:00:00", it correctly outputs:
start date: 26.3.2012 00:01
end date: 26.3.2012 23:59
Using mktime instead of Zend_Date works correctly in both cases. Is it bug in Zend_Date? I think it is, so I have already posted a bug report http://framework.zend.com/issues/browse/ZF-12121. But maybe I am missing something obvious?

I've just find this on stack overflow, it perfectly fixed my issue (same as yours)
See Bug in Zend_Date (back in time)
Good luck

When testing the code with just hardcoding the date: $date = new Zend_Date('2012-03-25 4:00:00', 'YYYY-MM-dd H:mm:ss'); the result is ok. Try to see whether the date output is the same when using $date->toString('d.M.yyyy HH:mm');

Related

My selected date is showing one day before in Ionic 4

I am working in my Ionic 4 app and I have used a native datepicker for that and I am converting the date to toISOString() but the problem is that it is showing one day before.
This is my ts:
this.datePicker.show({
date: new Date(),
mode: 'date',
androidTheme: this.datePicker.ANDROID_THEMES.THEME_HOLO_DARK,
}).then(
date => {
me.acceptchallengeform.setValue({
startchallenge: new Date(date).toISOString().split('T')[0],
});
console.log('Got date: ', date)},
err => {
console.log('Error occurred while getting date: ', err)}
);
var DatePickerDate ='Thu Aug 09 2018 00:00:00 GMT+0100 (British Summer Time)';
var myDate = new Date(DatePickerDate).toISOString().split('T')[0];
so myDate is now 2018-08-08
The problem is that it is showing the date one day before.
But I want to show the exact selected date.
Any help is much appreciated.
new Date() return the date using your system timezone.
toISOString() return the date in UTC and the time part of your date is midnight, so it returns Thu Aug 08 2018 23:00:00 (BST - 1h), that's why you're getting one day before.
What are your trying to do exactly?
Editing my answer following comment
in your case I would choose the easy way as you apparently don't need any timezone information:
startchallenge: date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
or you can use moment.js (http://momentjs.com/docs/#/displaying/format/)
Try This:
date = new Date(date.setDate(date.getDate() + 1));
me.acceptchallengeform.setValue({
startchallenge: new Date(date).toISOString().split('T')[0],
});
This will solve your problem.

groovy next() date issue

I am trying to add a groovy script in SoapUI to find tomorrow's date using next() in current date.
I am getting the date as expected for all other dates except if the date is 19.
def TodaysDate = new java.util.Date().format("yyyy-MM-dd")
log.info ">>>>>>>>>> TodaysDate="+TodaysDate
log.info TodaysDate.next()
Output:
Wed Jul 19 14:34:29 EDT 2017:INFO:>>>>>>>>>> TodaysDate=2017-07-19
Wed Jul 19 14:34:29 EDT 2017:INFO:2017-07-1:
I tried this also.
def Today = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date())
log.info Today
NextDay = Today.next()
log.info NextDay
Output:
Wed Jul 19 14:43:38 EDT 2017:INFO:2017-07-19
Wed Jul 19 14:43:38 EDT 2017:INFO:2017-07-1:
This next() iterator works fine for other dates. Can you help me understand what I am doing incorrect here?
The format() method returns a String. And when you call next() on a String, it increments the last character. So, character 9 is incremented to the next unicode value, becoming :.
If you want your dates in a specific format, first you call next() in a Date object, then you format it:
def TodaysDate = new java.util.Date()
log.info ">>>>>>>>>> TodaysDate="+TodaysDate.format("yyyy-MM-dd")
log.info TodaysDate.next().format("yyyy-MM-dd")
The will print TodaysDate=2017-07-19 and the next date as 2017-07-20.
Maybe it's worth using TimeCategory in your case? Take a look at this simple code sample:
import groovy.time.TimeCategory
use (TimeCategory) {
println new Date() + 1.day
}
It works fine with any date. Test it with today's date (2017-07-19) - adding 1.day will give you 2017-07-20. Hope it helps.

Groovy Date code producing unexpected output

Why is below code producing output like this ?
String oldDate = "2013-12-05 01:34:54.270"
Date date = Date.parse( 'yyyy-mm-dd hh:mm:ss', oldDate )
Output : Sat Jan 05 01:34:54 EST 2013
When it should simply produce December 5 as the date in the output ? In fact in the original string whatever month I put in it produces the exact same output.
The problem is your date format isn't correct. mm is for minutes where MM is for month. So it should be:
String oldDate = "2013-12-05 01:34:54.270"
Date date = Date.parse( 'yyyy-MM-dd hh:mm:ss', oldDate )
You can find out more from the Java API docs for SimpleDateFormat.

Problem in getting week number of sundays , in zend

I want to get the week number of a particular date , using Zend_Date
My local is setted as English(IN) [en_IN], in Opera browser
I am using the following code
$date = new Zend_Date('22 Mar, 2010', null, Zend_Registry::get('Zend_Locale'));
echo $date->get(Zend_Date::WEEK); //output 12, correct
But if we give a sunday , it will not work correctly
for example
$date = new Zend_Date('21 Mar, 2010', null, Zend_Registry::get('Zend_Locale'));
echo $date->get(Zend_Date::WEEK); //output 11, not correct
it should output 12
What is wrong with this?
For the Locale of English (India), 'en_IN', the first day of the week is Monday. Zend_Date is giving you the correct value.
EDIT: I just did a quick test using the 'en_US' locale, and I'm getting the same behavior. It looks like Zend_Date may be ignoring the locale for this calculation.
$locale = new Zend_Locale('en_US');
Zend_Registry::set('Zend_Locale', $locale);
$date = new Zend_Date('2010-03-22', null, Zend_Registry::get('Zend_Locale'));
echo $date->get(Zend_Date::WEEK); //outputs 12, correct
$date = new Zend_Date('2010-03-21', null, Zend_Registry::get('Zend_Locale'));
echo $date->get(Zend_Date::WEEK); //outputs 11, not correct
This should work fine:
$locale = new Zend_Locale('en_IN');
Zend_Registry::set('Zend_Locale', $locale);
$date = new Zend_Date('22 Mar, 2010', null, Zend_Registry::get('Zend_Locale'));
echo $date->get(Zend_Date::WEEK);
If not, try to determine it directly, using Zend_Locale_Data. Sometimes data provided by Unicode are not exactly as we expect for our region.

Zend_Date::toString() outputs the wrong year. Bug in my code, or Zend_Date?

I'm using Zend_Date to set and get the year, but it is not being set as the correct year. I set the year as 2010, and it returns the year as 2009. What am I doing wrong? Is there a bug in Zend_Date?
$date = new Zend_Date('2010-01-03', 'YYYY-MM-dd');
echo $date->toString('MMMM d, YYYY');
//outputs January 3, 2009
The year must be being set correctly because getting the year part of the date works:
echo $date->get(Zend_Date::YEAR); //2010
Solution:
Well I got it to work...You have you use lowercase: yyyy
echo $date->toString('MMMM d, yyyy');
YYYY stands for the ISO Year. 2010-01-03 is week 53, day 7 of the ISO year 2009
yyyy stands for the actual calendar year.
I've ran into this problem as well.
In the Zend_Date class 'YYYY' means to a 4 digit representation of the 'ISO year' where as 'yyyy' means a 4 digit representation of the 'year'.