PHP strtotime not recognizing "2012-W11" on new server - php

Sorry if this seems like a stupid question. I'm probably missing something obvious but I've been struggling with this for ages and am getting nowhere. I've tried searching but haven't been able to find anything specific enough so I'm asking here in the hope that someone smarter than me can help me out.
I'm using the following line of PHP code to get the timestamp of the beggining of the current week.
strtotime(date('o-\\WW', time()));
This works fine on my local server and it worked fine on my live shared hosting, but now that I have moved my site to a virtual dedicated server it returns a blank.
The date() part is working fine on all servers ie.
date('o-\\WW', time()) today returns 2012-W11 on all servers
Therefore the problem is with the way strtotime turns 2012-W11 into a timestamp ie a problem with running strtotime('2012-W11')
I'm guessing there must be a difference in the way the new virtual dedicated server is set up that is causing it not to be able to do this in the expected way but I can't for the life of me work out what it is.
Thanks in advance for your help

try changing the format to strtotime('2012 +11 weeks')

you can use below code get first day of the current week
$weekFirstDay = strtotime('Monday this week');
it's from PHP Manaul

Related

strtotime() not converting correctly in wordpress

I am facing an issue in strtotime() in wordpress, that it is subtracting the hours according to my configured local timezone.
For example i tried strtotime("2021-11-16 00:00:00") on online php compiler it shows...
1637020800
but when i run the same on my wordpress site, var_dump(strtotime("2021-11-16 00:00:00")); it shows:
1637002800
which is wrong it is subtracting 5 hours as i set GMT+5 in my general settings.
I tried current_time('timestamp') but it won't help
Please can anyone help?
i found the problem in wordpress version 5.4.2, so basically in time zone if you select city,country it shows you this problem.
While if you select like GMT +5 etc it will show correct.
So i change Karachi to GMT+5 which make it sorted.

get time and date in php

i am using
date_default_timezone_set('Africa/Nairobi');
$date = date('Y-m-d H:i:s');
but its always returning date and time form my PC.
Ex.. today is - 15/07/2015
and i changed my PC date to 17/09/2016
so php Date also returning same date...(17/09/2016)
is there any why to get real time and date?
what i have tried
1. simple date function
2. set timezone
3. i have searched on google but no luck yet...
It will always return your PC date as it well should.
Date & time functions use the server's date and time. So if you're running a local server (WAMP, XAMP or whatever) your PC will be the server and therefore it's time will be used.
Setting the timezone should change the time accordingly though.
There is nothing wrong with your code
date_default_timezone_set('Africa/Nairobi');
$date = date('Y-m-d H:i:s');
but as #Rizier123 said that if you are using XAMPP or WAMP or any other local server it will show the system time only. I would like to suggest you that put your code to an online server or use some online php compiler, then it will surely gonna give you the expected output.

Time Format for Filemaker PHP

I'm having an issue with the FileMaker PHP API. I don't know how to format a time correctly for input into a Time field.
I tried entering a string with the format that the documentation guide says (H:M where H can be 24 hours) and it still doesn't take. I don't know if I'm supposed to change the data type with another function like date() or what.
Edit: A little snippet of code
//Date
if($arrDate!=NULL){
$booking->setField('arrival_date', '07/17/2015');
}
//Time
if($arrTime!=NULL){
$booking->setField('arrival_time', '11:00:00');
}
$savedBooking = $booking->commit();
I replaced the variables for the value fields with what they should be just in case it was something going wrong with them, but it still doesn't work. The date field setting works fine if I comment out the Time section however.
Here's the format that I use:
$booking->setField('arrival_date', date ( "n/j/Y H:i:s A" ) );
If you're interested in adjusting the time, here's a post to checkout: http://timdietrich.me/blog/php-timezone-arithmetic/
Good luck!
I suspect you may have some validation set up on the "arrival_time" time field. What is the error that is returned by the API? It should return an error code that will tell you more.
Also worth mentioning that FileMaker will inherit the date and time settings from your system settings. For example, date formats are different in Europe than they are in the states. But that time value you give looks like it would work, and you only get an error when setting that field, so I would look to make sure the field is defined correctly.
You can also test by putting that value in via FM Pro and see what happens.
I'm an idiot, after hours struggling with this I realized I had put the wrong field name! Thanks for your help guys. A string of H:M worked perfectly.

php datetime custom formatting

I'm trying to use
format("m/d/y \a\\t h:ia T");
to get
05/14/15 at 04:22am MDT
which used to work but now all of a sudden I'm getting
Exception: DateTime::__construct() [datetime.--construct]: Failed to parse time string (05/14/15 at 04:22am MDT) at position 9 (a): The timezone could not be found in the database in some/folders/and.files on line x
So my work around for now is taking the "at" out
format('m/d/y h:ia T');
I've found I can leave the \a in with no problem but the \a\\t causes the error. Any on how to get past this?
**edit
The timestamp being passed in is generated by mysql
2015-05-14 10:22:41
**edit
This accepted answer didn't actually solve the problem for me. I am certain the issue is unique to my server / application so once I figure it out on my own I'll post back here as to avoid any further downvotes.
I'm working with a LOT of data and a LOT of files (classes) to get this thing working so I couldn't paste all my code in here or else no one would look at this question. So here's the skinny on what was actually happening:
Inside of a loop my time value was getting replaced with the formatted value and then getting caught up in the same loop, hence why I was getting that error and everything thought I was getting time with an incorrect value because ultimately I was (the first time it was okay but not on the re-run).
resolution
When in doubt, print out the time value going INTO your DateTime->format and you may be surprised that it's not what you think it is.

how to set date and time not depending on computer date and time?

Please help,I want to set my webpage date not based on computer time because what if the user's time is not set correctly. i have tried searching in google but unfortunately did not get my answer.
I tried this code but when i change my pc date and time it also changes the output in my webpage to my pc time.
<?php
date_default_timezone_set('Asia/Manila');
$timezone = date_default_timezone_get();
echo date("Y/m/d H:i:s");
?>
This would hardly be a problem, as the website code is run on the website server, which has its very own time and date setting. As long as you make sure the date/time setting of the server is correct you will be fine.
The date/time setting of visiting users sure can differ a lot, depending on the user's geographical location. It may cause trouble when running JavaScript on a webpage, but for server code (e.g. PHP) it would be irrelevant.
The date will be set relative to the server that runs the code, if you run your code with a server instance on your computer, changing your computer date settings will afect your script but if you use a server wich is not on your computer the date setting that will be returned by your script will be the setting of your server not your computer's.

Categories