why do i need to use strtotime() to use date() with mysql - php

I am using this code successfully, but is there a way to run this where I don't need to convert my variable from a string to a number?
I am confused and wondering if I am doing it efficiently because the mysql timestamp datatype is a string but the php timestamp is a number. There should be a date format conversion for the mysql timestamp,no?
date(DATE_ATOM, strtotime($program->start_time))
the date function takes a timestamp long datatype but mysql stores timestamps in a datetime string format. Is there another way to do this where I just convert once?

I don't know why do you need to change the dates in your code.
In Laravel base Model, dates works as Carbon objects. When that fields are stored in database, they're mutated to database date format. That's because date fields have their owns getter/setters in base model.
You don't need to change your custom date fields formats, only declare them in the $dates Model propertie so it can mutate that fields when they are stored in database.
For example, in this User model class the attribute start_time will be mutated from Carbon to MySQL format when it will be saved.
Class User extends Eloquent
{
protected $dates = ['start_time'];
}
You can read more about that here http://laravel.com/docs/4.2/eloquent#date-mutators
I expect that would help you to understand it better.

Related

How to compare simple date with datetime in Laravel?

On my project, I created an option for the user to search by date, but in my database, all the inserts are in DateTime format (yyyy-mm-dd h:m:s), while the search is made only with a simple date(yyyy-mm-dd) input.
Is there any way I can compare these two values, so the user can find all the inserts on the given date?
you can use eloquent whereDate with format Y-m-d
YourModel::whereDate('created_at','2021-11-24')->get();
or without model
DB::table('your_table')->whereDate('created_at','2021-11-24')->get();

Storing timestamp in MySQL

I'm grabbing data from a webpage that returns a timestamp in the following form (which is a string):
2013-11-09T15:14:48.957604
How can I interpret this in PHP, and what is the best way to store this in a MySQL database?
The best way to store it is to use the MySQL DATETIME data type. It is specifically meant to handle date/time values, and it works beyond the year 2037, which is the approximate limit using unix timestamps. MySQL and PHP both handle these values with ease using built in functions/libraries (for PHP, see the DateTime class as mentioned by another commenter).
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
http://php.net/manual/en/class.datetime.php
I just want to expand on the information given here.
This will convert the string into a DateTime object.
$dtObject = new DateTime("2013-11-09T15:14:48.957604");
But, in order to store the timestamp in the database there are several requirements.
Needs to be in Y-m-d H:i:s format.
MYSQL column type must be datetime/timestamp
Needs to be a string.
Now in order to get this timestamp into a MYSQL friendly format we need to use the format function.
$timestamp = $dtObject->format('Y-m-d H:i:s');
You can now INSERT this into the database. You can also output the same way following these date formats.

Using Laravel and Carbon

I have a field in my database called "deadline" which is of DATE format, and I want to use Eloquent to say that if the deadline field does not match Carbon::now(); but isn't in the future then don't show this row.
How can I achieve this?
Select records where deadline is greater than or equal to today's date() in the Y-m-d format.
Model::where('deadline','>=', date('Y-m-d'))->get();
Laravel works with Carbon for formatting timestamps etc, which you can easily set the deadline column as one of those Carbon objects with a Laravel date mutator allowing you formatting abilities.
But for a select statement, i'd just use the above personally.

MySQL and PHP working with user timezones

I store time in UTC timezone in MySQL DATETIME field.
I want to convert all these (when using them in PHP) to user defined timezone.
I know I can do it with php DateTime::setTimezone, but it would require to do it each for each one of them.
Is there some global way to tell php that all datetimes from mysql are UTC and convert them automatically to the user timezone?
When you store them into the database, store the table column as a unix timestamp. These are all UTC. Then just apply whatever user specified offset you want.
Here is an interesting StackOverflow question which is likely to help you with it.
You can also use the MySQL Unix_Timestamp function to query data easily by generating the right int when doing comparisons.
Edit:
If you are certain that you want to convert all the datetimes from the database each time you use them, just write a simple function that gets the datetime and adds the correct offset for the user, then simply call that function each time you pluck a datetime from the database. like the code below:
<?php
// Assuming that $_SESSION['UserOffset'] is storing the user's timezone eg
// $_SESSION['UserOffset'] = new DateTimeZone("Europe/Prague");
function convert_time_zone($timeFromDatabase_time)
{
$userTime = new DateTime($timeFromDatabase, new DateTimeZone('UTC'));
$userTime->setTimezone(new DateTimeZone($_SESSION['UserOffset']));
return $userTime->format('Y-m-d H:i:s');
// or return $userTime; // if you want to return a DateTime object.
}
?>

Symfony / Propel - retrieving old dates from a database

This is regarding Symfony 1.4, and probably all prior versions, running php 5.3, mysql 5.1.
If I store old dates in a database < 1970..., and I then retrieve them, they are automatically converted into an incorrect date.
Example table:
id, some_date
1, 1961-09-09
A quick example.
$record = MyTablePeer::retrieveByPK(1);
echo $record->getSomeDate();
//returns: 09/09/61
//Want it to return: 1961-09-09
Where is the output format controlled from? I need to get the entire date with the entire year stored in the database.
Date field getters have a $format parameter, use that, like $record->getSomeDate("Y-m-d");.
Also, this is what the docblock says about date accessors:
This accessor only only work with unix
epoch dates. Consider building with
propel.useDateTimeClass or change this
column type to the (deprecated)
"before-unix" column type (e.g.
BU_TIMESTAMP or BU_DATE) if you need
to support pre-/post-epoch dates.
#param string $format The date/time format string (either date()-style or strftime()-style).
If format is NULL, then the integer unix timestamp will be returned.
#return mixed Formatted date/time value as string or (integer) unix timestamp (if format is NULL).
#throws PropelException - if unable to convert the date/time to timestamp.

Categories