Asked  7 Months ago    Answers:  5   Viewed   32 times

How can I get the timestamp of 12 o'clock of today, yesterday and the day before yesterday by using strtotime() function in php?

12 o'clock is a variable and would be changed by user.

 Answers

15
$hour = 12;

$today              = strtotime($hour . ':00:00');
$yesterday          = strtotime('-1 day', $today);
$dayBeforeYesterday = strtotime('-1 day', $yesterday);
Wednesday, March 31, 2021
 
pinaki
answered 7 Months ago
16

Due to a daylight savings shift, 28/10/12 has an extra hour in your timezone. There are 25 hours between midnight 28/10 and midnight 29/10.

You will also find a day with 23 hours in spring.

If this is not what you expect, change the timezone to something that has no DST. UTC is one option:

php > echo mktime(0,0,0,10,29,2012) - mktime(0,0,0,10,28,2012);
90000
php > ini_set('date.timezone', 'UTC');
php > echo mktime(0,0,0,10,29,2012) - mktime(0,0,0,10,28,2012);
86400
Wednesday, March 31, 2021
 
SilverHorn
answered 7 Months ago
51

As @Petah said in the comments, store your times in UTC and covert them in the application as needed.

Unix timestamps are in UTC so I usually store my times in the database as timestamps. This saves the headache and confusion of first converting to UTC to insert, and then from UTC when selecting.

That is, make your time field an INT type, and use the function UNIX_TIMESTAMP() in MySQL when you insert, or get the timestamp from PHP using the time() function.

When you fetch the timestamp from the DB it will be in UTC, but when you display it in your PHP application using date(), it will display in the server timezone, or whatever you set with date_default_timezone_set.

Therefore the following two queries will work:

INSERT INTO `table` (id, time) VALUES(NULL, UNIX_TIMESTAMP());

// or

$time = time();

$query = "INSERT INTO `table` (id, time) VALUES(NULL, $time);

If you want to select it from the DB as a DATETIME, you can do this:

SELECT *, FROM_UNIXTIME(time) as dt FROM `table` WHERE 1

The resulting dt column will be in the format yyyy-MM-dd hh:mm:ss.

You can format the numeric timestamp in PHP using date()

If the PHP version you have is 64-bit, you aren't limited to the 1970 - 2036 range, PHP will support 64-bit timestamps, just make sure to use a BIGINT column in MySQL in that case.

Hope that helps.

Saturday, May 29, 2021
 
capsid
answered 5 Months ago
52

hope i got you right on this.

try it with Fetch all

$rows = $query->fetchAll(PDO::FETCH_ASSOC);

in your case

$sql='select uid,name from users where username=? and password=?';
$query=$con->prepare($sql);
$result=$query->execute(array($username,$password));


// check if a single row is returned
// i guess to avoid the warnings you can just set the array befor like
// $row = array();
// or put @ befor the VARS in the TRUE statement

$rows = $query->fetchAll(PDO::FETCH_ASSOC));

/** 
if you have more than one result you can look at them like this

foreach ($rows as $row)
{
    echo $row['uid'];
}
**/

if((count($rows)===1)
    {
        echo $_SESSION['id']=@$rows['uid']; 
        echo $_SESSION['name']=@$rows['name'];
        // header('Location: lading_page.php');
    }
else echo "Invalid username/password";
Saturday, May 29, 2021
 
RompelStompel
answered 5 Months ago
89

You can use array_intersect to cover the dynamic $data as such:

$data = array (
  'role_1' => 
  array (
    0 => 'value_2',
    1 => 'value_3',
  ),
  'role_2' => 
  array (
    0 => 'value_1',
    1 => 'value_2',
  ),
  'role_3' => 
  array (
    0 => 'value_2',
    1 => 'value_3',
  )
);

$result = call_user_func_array('array_intersect', $data);

call_user_func_array will help spread the elements of your array as parameters inside array_intersect.

Tuesday, August 17, 2021
 
Markol
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :