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?
Related
I added this code to my functions.php from stack overflow to manage my upload folder by date (2019/08/14) instead of by month
function upload_dir_filter($uploads){
$day = date('d');
$uploads['path'] .= '/' . $day;
$uploads['url'] .= '/' . $day;
return $uploads;
}
add_filter('upload_dir', 'upload_dir_filter');
Then month change, I got a folder on 31 August 2019. Instead of 31 July 2019. I realize that this code is using UTC even though I set my VPS to Australia/Melbourne time.
I already change the timezone in the WordPress admin (via web browser) but no effect
How do I change WordPress PHP code timezone?
Note: I leave date.timezone in PHP blank (or didn't set it to anything at all) because default VPS timezone is already Australia/Melbourne
Edit:
After some thought, the wordpress and bundles (php, etc.) setting is correct because my timezone is UTC+10 (ahead of GMT)
The time are already in August 1 but this particular code is stuck in July 31.
How do I set timezone for this particular code only? Is this correct? - not affecting timezone in other codes?
function upload_dir_filter($uploads){
date_default_timezone_set('Australia/Melbourne');
$day = date('d'); -----> Only want this to return in 'Australia/Melbourne' time without affecting other or general wordpress setting
$uploads['path'] .= '/' . $day;
$uploads['url'] .= '/' . $day;
return $uploads;
}
add_filter('upload_dir', 'upload_dir_filter');
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'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.
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
I made a Diary at http://kees.een-site-bouwen.nl/agenda which can have Events for specific dates. You can add an event using the form located here: http://kees.een-site-bouwen.nl/evenementform
As you can see I have 3 fields 'Datum, Van & Tot' (translation: Datum = date, Van = From, Tot = till)
If the time on that specific date expires I would like to run a script which deletes that specific row from the database.
I searched on google and found a few things like MYSQL Trigger and PHP cronjob, but I don't know if there's an easier way to do this? or how to do it exactly.
My database structure looks like this:
agenda // diary
- - - - // not added the whole structure.
idagenda
titel
waar
www
email
activated
....
....
agendadatum // diary dates
- - - - - -
id
datum
van
tot
idagenda
as you can see I'm using a join to add more dates to one event.
How could I trigger the datetime to delete the rows from the db if the date = today?
NOTE: I am using the Codeigniter framework.
You could set a hook. And use a function like
$this->db->where('tot <', date('Y-m-d H:i:s'));
$this->db->delete('agendadatum');
My codeigniter is a bit rusty but that query should remove all "old" entries on EVERY page load. So if you're going for high traffic this will not hold up.
Running a cronjob every hour/day is probably the "cleanest" way. This will require you to set a where condition on all selections of agendadatum that forces the "tot" date to be in the future. Else it's possible you see expired entries.
Edit:
From what I can gather if you define your hook like:
$hook['post_controller_constructor'] = array(
'class' => '',
'function' => 'delete_old_entries',
'filename' => 'agenda.php',
'filepath' => 'hooks',
'params' => array()
);
And create a file named agenda.php in application/hooks which looks like:
<?php
function delete_old_entries() {
$CI =& get_instance();
$CI->load->database();
$query = $CI->db->query(" DELETE FROM agendadatum WHERE tot < NOW(); ");
}
It should work. However this is pieced together from what I could find and untested. So it might work, it might not. But something along these lines should do the trick even if it isn't this.
If I understand correctly:
CREATE TRIGGER deleteRows AFTER UPDATE,INSERT ON myTable
FOR EACH ROW BEGIN
DELETE FROM myTable WHERE Datum = NOW()
END;
MySQL has a built-in Event Scheduler that basically allows you to run arbitrary SQL code at scheduled times. You could purge your database once a day for instance.
However, I know from your other question that you are hosting your project on a shared host, and, unfortunately, shuch hosts often disable the feature. Check out this manual page to find out whether the feature is active on your server.