Mysql date("") with EST timezone? - php

So I went into my.ini and added
default-time-zone="-5:00"
Under the [mysqld] reset xmapp and when I add a person to my database it is still inserting into the database for date as 2/14/23 04:12:00? I should be set to EST since I put -5:00
This is what is on my php side not sure if I need to add a timezone some where here also or not.
, "date" => date("Y-m-d H:i:s") ,
I haven't tried anything other than add the default-time-zone in my.ini
Anyone help with this since everything I looked up on google says to edit that file and it should work.
What I want it to do though is when it inserts the new person I would like it to do it for EST.
This would be what my code looks like
$stmt = $pdo->prpare("INSERT INTO `table`(`name`,`branch`,`startDate`) VALUES ( :name, :branch, :date) ");
$stmt->execute(["name" => $_POST["name'], "branch" => $_POST["branch"], "date" => date("Y-m-d H:i:s") , ]);

Someone answered this, by saying to add
date_default_timezone_set('America/New_York');
Which worked. Not sure what happen to their answer, but that was my solution!

Related

How to check current date record in laravel?

I am trying to check game_id only for today or current date and for this I am trying following script
$todayExist = Training::whereRaw("Date(created_at)=Curdate() and game_id=$game_id")->get();
if($todayExist->count() > 0 ){
$error = 1;
$result = false;
}
The above query output is
select * from `games_training` where Date(created_at)=Curdate() and game_id=6
But for game_id=6 there is duplicate entry, as it was generated by 3 hours ago (Dubai Time).
So can someone kindly guide me what can be the issue? Is it wrong query or it happened because server timezone.
I just insert record, in server database it is showing 09:31:57 which mean is 09:31 AM, however I am in Dubai and right now here is 1:33 PM.
Can someone kindly guide me about it.
Thank you so much.
Laravel 5.6+ users, you can just do
$posts = Post::whereDate('created_at', Carbon::today())->get();
Besides now() and today(), you can also use yesterday() and tomorrow() and then use the following:
startOfDay()/endOfDay()
startOfWeek()/endOfWeek()
startOfMonth()/endOfMonth()
startOfYear()/endOfYear()
startOfDecade()/endOfDecade()
startOfCentury()/endOfCentury()
OR
Using query builder,Use Mysql default CURDATE function to get all the records of the day.
$records = DB::table('users')->select(DB::raw('*'))
->whereRaw('Date(created_at) = CURDATE()')->get();
dd($record);
Go to
config -> app.php and change 'timezone' => 'Asia/Dubai'
.
Then
php artisan config:cache
You can find the timezone configuration in "config -> app.php" just change 'timezone' => 'UTC', to 'timezone' => 'Asia/Dubai',and restart your XAMPP.
The current date means what for you? Because you have the mysql server timezone, your lavarel timezone setting and your computer's timezone setting ...
When you write: Date (created_at) = Curdate (), Curdate () examines the time zone of the mysql server.
So what is the "current date" you want to find ? Remember that every user in the world has their own "current local date" while the date stored in your mysql database will only be a fixed date (it is better to be UTC for more reliability ... )

how to save time with AM or PM included in it?

i'm working on project where i'm doing this piece of code
date_default_timezone_set('Asia/Calcutta');
$login_time=>date('h:i:s');
now is there anyway i can include AM/PM value in my database? or i'm missing any step to do that :|
If you want to save with AM or PM, based on your given code , it should be written with 'A' inside date function like this:
date_default_timezone_set('Asia/Calcutta');
$login_time=>date('h:i:s A');
A = Uppercase Ante meridiem and Post meridiem.
Read more about date function here.
Though I'd recommend using the database to format your date, it is also possible using the PHP Date function. To wit:
$login_time_as_string = date ("h:i:s A", $login_time);
If you need more assistance, do leave a comment.

date in MySQL using PHP and it doesn't seem to be working

I searched the site already to see if this question has been addressed already and found no results, but if it has then I appologize and if you can link me to that thread I'll refer to that to solve my problem.
I am trying to save a date in MySQL using PHP and it doesn't seem to be working. The field in MySQL is the "date" type and when I read data from that field it shows as 0000-00-00, so I have tried saving the data as 2009-09-24 for example, but it doesn't seem to work. I have tried saving it with quotations to make it a string, and without quotations and I still can't seem to figure it out.
Does anyone know how I can format the date propely so I can save it in MySQL?
Thank you very much!
Try using the following where $t is a timestamp (time()) or nothing
function MysqlDate($t=0)
{
if ($t==0)
return date("Y-m-d H:i:s");
else
return date("Y-m-d H:i:s", $t);
}
var_dump(MysqlDate());

Editing server time for a PHP script

I have changed the way i save the time when a log is submitted to the DB by using the 'datetime' stamp in MySQL.
Im saving it in this format date("Y-m-d G:i:s"); which outputs it like so: 2012-02-22 20:20:01
The only problem is im in the UK, and my hosting/server isnt.
I have found out how to edit the time to bring it inline with UK time by doing this:
date = date("Y-m-d G:i:s", strtotime("+14"));
So now i have the time right i thought i would be ok, but now i have noticed that the date is not inline with UK date/time.
If i did a submission now it would display: 2012-02-22 20:25:36 when the time/date is actually 2012-02-23 20:20:01.
So it looks like that because my script is 14 hours behind, its knocking the date out to.
Is there a way i can fix it ?
Thanks, Sam!
include this in your program
date_default_timezone_set('Europe/London');
Add this one line in your php.ini file:
date.timezone = "Europe/London"
Now, anytime you use date or time functions, it will be adjusted to what you have specified.
For a complete list of timezones in Europe:
http://www.php.net/manual/en/timezones.europe.php
Utilizing the PHP Datetime module: Supported Timezones
$tz = new DateTimeZone('Pacific/Kiritimati'); // Change this to YOUR needed Timezone
$datetime_GMT = new DateTime(strtotime($server_or_SQL_date));
$datetime_GMT->setTimezone($tz);
$datetime_GMT->format("Y-m-d G:i:s");
echo $datetime_GMT;

How to insert a proper date into a mysql query

I was using this query to fill my values:
mysql_query("INSERT INTO 'drivers'(coupon,loyalty,etairia,package,pump,date,merchant,public,private,
amount,plate,nonce)VALUES('".$_REQUEST['coupon']."','".$_REQUEST['loyalty']
."','".$_REQUEST['etairia']."','".$_REQUEST['package']."',0,NOW(),'".$_REQUEST['m']."
','".$_REQUEST['pu']."','".$_REQUEST['pr']."','".$_REQUEST['amount']."',
'".$_REQUEST['plate']."','".$_REQUEST['nonce']."');");
This is working fine, but with NOW() I have the server hour so I want to convert it to my local hour.
I found this on another question:
$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/Athens'));
$fdate = $date->format('Y-m-d H:i:s');
I printed it and it returned the correct hour.
Finally I tried to put it inside the query instead of NOW() but when I run it it doesn't even make a row to my base.
This is my code now:
mysql_query("INSERT INTO `drivers`.`pay`(coupon,loyalty,etairia,package,pump,date,merchant,public,
private,amount,plate,nonce)VALUES('".$_REQUEST['coupon']."','"
.$_REQUEST['loyalty']."','".$_REQUEST['etairia']."','".$_REQUEST['package']
."',0,'".$fdate."','".$_REQUEST['m']."','".$_REQUEST['pu']."',
'".$_REQUEST['pr']."','".$_REQUEST['amount']."','".$_REQUEST['plate']."','"
.$_REQUEST['nonce']."');");
My php version is 5.5.9
To get local time:
echo date("Y-m-d H:i:s");
To get global time:
echo gmdate("Y-m-d H:i:s");
Or set your timezone something like this:
date_default_timezone_set('Europe/Athens');
print date('Y-m-d H:i:s')."\n";
I will suggest you, do not use mysql_.It is deprecated from the latest version of PHP.Use mysqli_ instead of this.
As has already been suggested, try using "date_default_timezone_set" and "date" to get the date in your local timezone.
I would also recommend a couple of other things:
Use mysqli instead of mysql functions as mysql functions are deprecated
Escape your strings! To avoid SQL injection use mysqli_real_escape_string on anything that comes from the request
I understand that your question is "what is wrong with this mysql query ?". The problem is that you don't see which error is produced by MySQL.
This case is known for PHP as a "WSOD" or White screen of death : nothing is displayed, generally because of some error setting (php function error_reporting).
If you take a look at this page, you will find a way to declare a error handler, which is a great time saver when programming PHP. You will also read the reason of your error and you then can explain it to us all. :-)
Check out UNiX_TIME stamp. It will store as a big int . It's basically seconds count from a particular date which the clock was set . It's a good way as it gives you flexibility in retrieving in any format you want. You can convert it in client side. Hope this helps
You can use date('Y-m-d H:i:s') or gmdate('Y-m-d H:i:s') to get the current date and time. You will need to make sure that your date column is set as a DATETIME type
I don't know if you still need it, but with the following code
$timezone = +1;
$date = gmdate("Y-m-j H:i:s", time() + 3600*($timezone+date("I")));
You can change the timezone as you want (for example my timezone is GMT + 1 where I am now) and with the date you also no need to worry about when daylight time changes.
For you is
$timezone = +2;

Categories