Get week number according to your locale in Zend_Date - zend-framework

I am trying to get the current week number with Zend Framework.
In France, weeks are defined like this :
A week begins on Monday (whereas weeks begin on Sunday in the US).
The first week of the year is the week which contains the 4th of January.
In 2014, the first week begins January 1st. But if I use in Zend Framework 1.12 for this date like $zend_date->get(Zend_Date::WEEK) it returns 53 (and not 1). and for January 12th, it returns 1 (and not 2)
How can I correct that ? I already tried to change the locale to fr-fr but it didn't work.
Regards

Tried to replicate your problem doing
$date1 = new Zend_Date('2014-01-01');
$date2 = new Zend_Date('2014-01-12');
$date1->get(Zend_Date::WEEK) // gives 01
$date2->get(Zend_Date::WEEK) // gives 02
Also tried passing 'fr' as locale. It gives the correct answer.
I am not sure if it's a Zend issue.
Try the PHP solution:
php > echo date('W',strtotime('2014-01-01'));
01
php > echo date('W',strtotime('2014-01-12'));
02

Related

How to enter a YYYYMM date in Jekyll?

How should dates be entered in Jekyll?
I would like to enter the date "August 2018" in a YAML file to be used in Jekyll. I find lots of information on how to format already-existing dates, but pretty much nothing on how to enter them.
The best I have managed to find is Date formatting, which implies that dates entered in ISO 8601 format should be valid. If I run with this, then Wikipedia explicitly states
"2004-05" is a valid ISO 8601 date, which indicates May (the fifth
month) 2004.
This implies that "August 2018" could be entered as 2018-08.
However, when I use my YAML file my_data.yml in my _data folder
date: 2018-08
then Jekyll doesn't recognize it as a date as
{{ site.data.my_data.date | time: '%B %Y' }}
outputs "2018-08" and not "August 2018".
TL;DR: Enter YYYYMM dates such as "August 2018" as Aug 2018.
Searching through the Jekyll repo, I found the date filters. The Ruby on Rails method .to_formatted_s (source) seem to be key to most of them. In the source to that method dates are written as Tue, 04 Dec 2007 00:00:00 +0000 from which I guessed that I should write Aug 2018. Doing so in my_data.yml, the code outputs the expected “August 2018”.

Perl workweek conversion is incorrect

I'm faced a weird problem.
I have date in form of Tue Feb 25 00:20:13 2014.
my task is to calculate the week number and the week day.
I tried the following
use Time::Piece;
my $date="Tue Feb 25 00:20:13 2014";
my $db_date=Time::Piece->strptime($date, "%a %b %d %H:%M:%S %Y");
my $ww=$db_date->strftime("%W.%w-%Y);
print $ww;
When I run the script I get the output as
08.2-2014
which is wrong, the expected output is
09.2-2014
I want to know where did i go wrong?
pls help...
You're using the "%W" strftime() conversion. Time::Piece doesn't specify the meaning of "%W", but the documentation for the equivalent C function says that "%W" starts counting with the first week that contains a Monday. It sounds like you want the ISO 8601 week number, which starts counting with the first week that contains at least four days, in which case the "%V" conversion should do what you want.

Powershell HowTo get last day of 2 month ago

I've a script scheduled every 4th and 14th day of month.
when script starts on 4th, I need to get the last day of the previous month (easy, $a.AddDays(-5))
when script starts on 14th, I need to get the last day of 2 month before.
for example:
14 april.
I want to get:28 february 2013
how is it possible?
also I want to get the date in format yyyymmdd
UDPDATE:
Is it possible that your solution doesn't work well with the change of the year?
if I'm in january, $(get-date).month -1 results 0. so I did this:
$datenow = Get-date
if $datenow.day -eq 14
$datenow.AddDays(-14)
then I calculate
$LastDayInMonth = [System.DateTime]::DaysInMonth($(Get-date).Year, $(Get-date.Month))
$datenow.AddDays(-$LastDayInMonth)
$datestring = $datenow.ToString("yyyyMMdd")
To get the date in a string:
$(get-date).Date.ToString("yyyyMMdd")
For getting the last day of two months prior:
if($(get-date).Day -eq 14){
$LastDayInMonth = [System.DateTime]::DaysInMonth($(get-date).Year, $($(get-date).Month - 2))
}
Update
The line
$LastDayInMonth = [System.DateTime]::DaysInMonth($(get-date).Year, $($(get-date).Month - 2))
Uses the static method DaysInMonth in the System.DateTime class to get the days in the month of the month that is passed to it. It's documentation is here. It takes as input two parameters, the month as an integer, and the year as an integer as well.
In our case, we want 2 months before this month, so for the month parameter we pass in
$(get-date).month - 2
And that is surrounded in a parenthesis to make sure powershell does the calculation and then pass the result of the calculation. get-date is a powershell cmdlet that gives the current date and time as a .NET dateTime object, so we have all the properties and methods at our disposal.
The whole
[System.DateTime]::DaysInMonth()
is just the way of calling static methods in powershell.
Update 2
Once you get the last day in the month, you can concatenate the string by:
$LastDayInMonthString = "$($(get-date).AddMonths(-2).ToString("yyyyMM"))$LastDayInMonth"
$(Get-Date).addDays(-$(Get-Date).Day).addMonths(-1).ToString("yyyyMMdd")

Date value as float - Apple's iWork Numbers 09 - internal XML

I am writing a script in PHP which will convert Numbers file into HTML table, but I can not figure out which format is used for date storage. The date cell tag looks like
<sf:d
sf:s="SFTCellStyle-128"
sf:w="84.074219"
sf:h="14"
sf:cell-date="371397519.99999952" />
so the date must be in sf:cell-date attribute, but I can not figure out how to convert it into human readable format. Any ideas? I have never seen date value as float number.
As written in a comment, it is the number of seconds since 01/01/2001 at 00:00:00. Equipped with that knowledge and because this goes hand in hand with the UNIX Epoch all you need to do is to define and use the offset. It should be compatible with nearly every of the existing PHP date functions and objects, for example with date:
define('CELL_DATE_EPOCH_OFFSET', 978307200);
$sf_cell_date = 371397519.99999952;
echo date('r', CELL_DATE_EPOCH_OFFSET + $sf_cell_date);
The output of this little script is (in my timezone):
Mon, 08 Oct 2012 15:58:39 +0200
I hope this is helpful. 978307200 is the unix timestamp for 01/01/2001 00:00:00 UTC, you can get with PHP for example with the following code-example:
$base = new DateTime('01/01/2001 00:00:00 UTC');
echo $base->getTimestamp(), "\n";
in case that was your problem.

Groovy date parse issue

date.parse() method of groovy detects date DD and year yyyy correctly but is unable to detect the month as mmm.. As in
println new Date().parse("DD-MMM-yyyy", '22-MAR-2011')
yields output as
Sat Jan 22 00:00:00 GMT+05:30 2011
Why is the month march as MAR picked up as Jan? What can I do to make it detect the month in mmm format?
The problem is actualy that you are using DD - that means day in year
Correct way:
println new Date().parse("dd-MMM-yyyy", '22-MAR-2011')
Quick tip when formatting dates try using the reverse and see what comes out:
println new Date().format("dd-MMM-yyyy")
Groovy uses SimpleDateFormat under the hood but that's not that important since most date libraries use the same format conventions.