Carbon get month index from month name - php

I'd like to get month index from month name using Carbon.
But I use Turkish month names.
I pass a query string to index like this ?ay=Temmuz&yıl=2017 so carbon should give 7 in this case.
The relevant part in my index function is like this:
public function index()
{
$gonderiler = Gonderi::latest();
if ($ay = request('ay'))
{
Carbon::setLocale(config('app.locale'));
$gonderiler->whereMonth('created_at', Carbon::parse($ay)->month);
}
if ($yil = request('yil'))
{
$gonderiler->whereYear('created_at', $yil);
}
/ ... /
}
When I click side bar and pass this query string it gives an error message like that:
"DateTime::__construct(): Failed to parse time string (Temmuz) at
position 0 (T): The timezone could not be found in the database"

on Linux
If you have trouble with translations, check locales installed in your system (local and production).
locale -a to list locales enabled.
sudo locale-gen tr_TR.UTF-8 to install a new locale.
sudo dpkg-reconfigure locales to publish all locale enabled.
And reboot your system. see documentation: http://carbon.nesbot.com/docs/#api-localization
For more you can refer to: Laravel Carbon localization not working (get localized name of month from number)
As the error you are getting is about Timezone not found in database.

Related

PHP Server time is behind local time

my server time is ahead of my local time (I cannot change my server time) by about 4 hours. The server is hosted in a different time zone, and changing the server time would have too many far reaching effects on too many other things.
Anyways, I have customers that place an order for a Gift Card for instance, and the gift card is dated to be sent out the same day that they ordered it, so the date stamp that gets placed on the Gift Card is (for instance), 1/3/2018.
But, let's say they placed the gift card order at 11:30 PM, and the server time has already moved onto 1/4/2018. My code below does not account for that:
$curDate = date('Y-m-d');
if ($card->getCardStatus() == 0){
if ((($card->getMailDeliveryDate() == null) || ($curDate == $card->getMailDeliveryDate())) && $card->getCardType() != 'offline') {
$card->setCardStatus(1);
$card->save();
$card->send();
}
}
Any ideas on how to take this scenario into account?
Thank you.
Edit Awh man, I got my question reversed around. My local time is ahead of my server time. I apologize. My Magento is set to eastern time (where I'm at) but the server itself is located in LA, thus PST. I don't want to change the timezone, I just want to make sure that orders placed in other time zones are accounted for.
This is where the PHP date_default_timezone_set() function steps in.
date_default_timezone_set — Sets the default timezone used by all date/time functions in a script
Set the default_timezone like this:
date_default_timezone_set('America/Los_Angeles');
More here http://php.net/date-default-timezone-set.
EDIT: As suggested by #RamKesavan, you can also set the timezone in Magento settings by doing this:
First set global timezone to GMT or UTC:
1.) Go to System -> Settings
2.) Edit your default scope
3.) Go to Configuration -> General -> General -> Locale Options
4.) Select GMT Standard Time (or UTC)
Then you need to set your default website scope to your preferred time zone like this:
1.) Go to System -> Settings
2.) Select the scope for each of your websites.
3.) Go to Configuration -> General -> General -> Locale Options again.
4.) Select W. European Standard Time (Europe/Berlin or Europe/Amsterdam) or select some other timezone appropriate for the location of your customers.
5.)Repeat for the next website.
The default website scope will ensure that your websites display Dutch, Chinese or American order and shipping times, or some other time according to your settings.
More here https://support.hypernode.com/magento-utc-hypernode.
EDIT 2: (OP changed the question)
It is impossible to detect the user date time locale settings with accuracy from the server side (PHP). This is what the client side (javascript) is used for.
There is a javascript script available jstimezonedetect which is used to easily detect the user time zone settings. Download the script and include it in the page where you have the purhase form.
To retrieve the timezone and send it with the purchase form you can add a hidden field inside the purchase form (it will be sent with the form) like this:
var tz = jstz.determine(); // call the jstimezonedetect script object
var tzName = tz.name(); // name of the user timezone 'Europe/Berlin'
var hiddenTimezone = document.getElementById('myHiddenTimezone');
hiddenTimezone.value = tzName; // add timezone value to hidden field
And in HTML you would need to have the hidden element added inside the purchase form like this:
<form ...>
// ...
// ...
<input type="hidden" id="myHiddenTimezone" name="userTimezone" value="" />
<!-- since the hidden element is inside the form and has a name -->
<!-- it will be sent with the form -->
// ...
</form>
On the server side (receiving side) you would get the timezone like this:
$userTimezone = $_POST['userTimezone']; // or $_GET['userTimezone'] if form method was GET
When a purchase occurs, you now have the server time and the user timezone. You can make 3 columns in your table. One for server time, another for user time zone and the third the current (calculated) user time.
You can then extract whichever column you wish and you would know for sure what you are getting from the database and how to write a PHP function to handle it.
EDIT 3:
To simply convert the a date from for example PST to EST you would do this:
$ESTDate = new DateTime('2017-02-04 12:32:43', new DateTimeZone('America/Los_Angeles'));
$ESTDate->setTimezone(new DateTimeZone('America/New_York'));
If you wish to echo the datetime with formatting:
echo $ESTDate->format('Y-m-d H:i:s');
If you wish to compare to another date you can use:
$difference = $ESTDate1->diff($ESTDate2);
which will produce results like this:
DateInterval Object
(
[y] => 3
[m] => 5
[d] => 15
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 1264
)
// usage
if($difference['days'] == 0)
// ...
Or you can use the > < and other operators like this:
// both dates must be in the same format (example: 'Y-m-d h:i:s')
if( $ESTDate1 > $ESTDate2) // date1 is greater
else // date2 is greater or equal

I am Facing issue in magento timezone when someone place order.

Timezone Issue is inside the order email.
I put My code which returns date like :
Code :
$created = Mage::helper('core')->formatDate($this->getCreatedAt(), 'long', true);
O/P :
March 31, 2016 9:10:33 AM EDT
But I want to Need Date Like Based on EST Timezone :
EST - Timezone with proper
So please any one can help me how can i achieve this and also first priority to magento code and second one PHP.
Thank You.
Login as admin and navigate to System -> Configuration -> General -> Locale Options -> Timezone and change timezone from there

How to validate DNSSEC in PHP?

I've been trying to figure out a way to validate DNS records in PHP (oa1) but have come up short.
I can validate a whole domain with this library, but not the individual records: https://github.com/metaregistrar/php-dnssec-validator
In addition, that library only allows for a very small set of TLDs to be validated.
Is there another library out there that can handle this for me, or perhaps something else I should look into?
I've also found this: http://www.phpclasses.org/package/9031-PHP-Validate-DNSSEC-keys-and-calculate-the-DS-record.html
But I have no idea how to get the keys to use in their validating function.
Help please!
UPDATE
So, I ended up using this...
exec('host -t RRSIG ' . $domain, $output);
Returns the RRSIG, or lack thereof, with minimal hassle.
The PHP engine has a fixed set of DNS record types it supports, all defined by the type parameter to dns_get_record. You can double check this list by looking in the engine code that implements DNS queries.
Unfortunately, none of the DNSSEC records are in that pre-defined list. So, you need to rely on a library or an external tool.
I'd use Net_DNS2, as it supports many DNSSEC RR. Example:
$google = new \Net_DNS2_Resolver(['nameservers' => ['8.8.8.8', '8.8.4.4']]);
$google->dnssec = true;
try {
$result = $google->query('kyhwana.org', 'SSHFP');
} catch(\Net_DNS2_Exception $ex) {
die($ex->getMessage());
}
foreach ($result->answer as $answer) {
if ($answer instanceof \Net_DNS2_RR_SSHFP) {
printf(
'%s %d %s %s %d %d %s' . PHP_EOL,
$answer->name,
$answer->ttl,
$answer->class,
$answer->type,
$answer->algorithm,
$answer->fp_type,
$answer->fingerprint
);
} else if ($answer instanceof \Net_DNS2_RR_RRSIG) {
printf('Signed by %s: %s' . PHP_EOL, $answer->signname, $answer->signature);
}
}
Aside: if your domain uses ECDSA algorithm or the SHA-256 fingerprint (like the example above), then you need the latest Net_DNS2 code which fixes Issue #39.
I haven't implemented the code yet but it looks to me like using php to verify the signatures themselves is a mistake. Use a local recursive resolver like unbound on the local server that enforces DNSSEC.
Then in php with the PEAR module Net_DNS2 look for an RRSIG record for the domain of interest. The result should tell you the zone responsible for that record.
Look for a DNSKEY record in that zone. If present, then look for a DS record.
The DS record will come from the parent zone (e.g. .email for deviant.email) and if present indicates DNSSEC is present for the zone.
If DNSSEC is present for the zone, then all the results for the zone are valid if your local recursive nameserver enforces DNSSEC.
I believe with Net_DNS2 that using
r = new Net_DNS2_Resolver(array('nameservers' => array('127.0.0,1')));
r->dnssec = true;
will also result in Net_DNS2 also validating the results if DNSSEC is active - which is indicated by the DS record that comes from the parent zone. But using a recursive resolver (preferably on the webserver) that enforces DNSSEC is probably safest.
Make sure the recursive resolver isn't listening on public interfaces so it doesn't get used in reflective amplification attacks.
Having the same goal (validate DNSSEC in PHP), i ended up with:
exec("drill -k root.keys -TDQ $domain SOA | grep -v '^;;'", $output);
This is doing a complete topdown DNSSEC validation.
You need root keys you can retrieve with:
unbound-anchor # NB: on FreeBSD, use local-unbound-anchor for "base" version
Or "manually":
dig . dnskey > root.keys
To confirm that the record is fully DNSSEC validated, check the last line of exec output:
[S] self sig OK This is not satisfying, probably zone key has not been populated in parent zone
[B] bogus
[T] trusted This means that everything is OK!

Translate month in Symfony 2 date field

I'm working on SF2 2.6.4 project.
I created a form and added a date type component (birthday field type to be precise).
I set it like that :
'widget' => 'choice',
'format' => 'ddMMMMyyyy',
Format output
18 March 2015
I would like to translate month.
During my research, I saw that Symfony\Component\Form\Extension\Core\Type\DateType class set a formatter with the hardcoded \Locale::getDefault() variable system.
So is there a way to do what I want ?
Is it a better idea to change my date format ?
Thx
Normally if you have your intl enabled in php (phpinfo) the form translate the months according to your locale configuration try setting locale like this before your form creation :
setlocale(LC_TIME, "fr_FR");
Another alternative if this doesn't help is this :
http://sonata-project.org/bundles/intl/master/doc/reference/datetime.html you can use the datetime helper

Common issue with php timezone in Joomla 3

I have a custom .php file that creates new Joomla articles with the desirable content. So, in my php script I have a connection to database, to jos_content table in order to create new articles.
One of the columns in SQL query is "created" and the value is a php variable $d (it is date).
date_default_timezone_set("Europe/Belgrade");
$d = date('Y-m-d H:i:s');
In GLOBAL CONFIGURATION > SERVER in Joomla 3, I set up a timezone to be also Belgrade.
Additionaly, I created a php.ini file in my root directory on server and add line of code of the particluar timezone
date.timezone = "Europe/Belgrade"
And after all this effort, the timezone is not applying. It is +2 again and obviously I have to change somewhere else, something else.
If I set up H:i:s to be 00:00:00 I got the result that my article will be published on today's date but at 02:00:00.
What am I missing?

Categories