Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

set time to 1 am today and yesterday?

Posted on 2011-10-13
4
Medium Priority
?
172 Views
Last Modified: 2012-08-14
time() returns the current date and time in Unix format.

How do I set a variable to 1 AM of the current day and 1 AM yesterday, in Unix format?
0
Comment
Question by:pillmill
  • 2
4 Comments
 
LVL 13

Expert Comment

by:George K.
ID: 36965063
try something like:
$mynewtime = time () - 3600;

---3600 seconds=1 hour
0
 
LVL 13

Expert Comment

by:George K.
ID: 36965070
Look also at the following -link:http://php.net/manual/en/function.date.php
<?php
$tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"),   date("Y"));
$nextyear  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y")+1);
?>
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36965213
Maybe this makes sense for you:

 
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');

/*get today date*/
$date = date('Y-m-d');

/*uses date and add the wanted hour then get timestamp and stores it in $datehour1*/
$datehour1 = strtotime($date.'01:00:00');

/*print the result*/
echo date ('Y-m-d h:i:s A', $datehour);
echo "<br>";
echo "<br>";
/*uses timestamp to perform operation: subtraction of 24 hours and stores the result in $datehour2*/
$datehour2 = $datehour - 60*60*23;
/*print the result*/
echo date('Y-m-d h:i:s A', $datehour2);
?>

Open in new window


$datehour1 and $datehour2 hold the unix timestamp of your values.

Cheers
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36968174
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

time() returns the current date and time in Unix format.
Not exactly.  time() returns the Unix timestamp, which is the number of seconds since the beginning of the Unix epoch.  The current date and time is location dependent; the Unix timestamp is not.

Location dependent information is set by date_defalt_timezone_set()
http://php.net/manual/en/function.date-default-timezone-set.php

So the question will be answered with strtotime() after you have told it where in the world you might be.  Test the script here:
http://www.laprbass.com/RAY_temp_pillmill.php
<?php // RAY_temp_pillmill.php
error_reporting(E_ALL);
echo "<pre>";


// SET OUR LOCATION
date_default_timezone_set('America/New_York');

// GET THE DATETIME VALUES INTO ISO-8601 STRINGS
$t = date('c', strtotime('Today     1:00 a.m.'));
$y = date('c', strtotime('Yesterday 1:00 a.m.'));

// SHOW THE ISO-8601 STRINGS
echo $t;
echo PHP_EOL;
echo $y;
echo PHP_EOL;
echo PHP_EOL;


// SET OUR LOCATION
date_default_timezone_set('America/Chicago');

// GET THE DATETIME VALUES INTO ISO-8601 STRINGS
$t = date('c', strtotime('Today     1:00 a.m.'));
$y = date('c', strtotime('Yesterday 1:00 a.m.'));

// SHOW THE ISO-8601 STRINGS
echo $t;
echo PHP_EOL;
echo $y;
echo PHP_EOL;
echo PHP_EOL;

// SHOW THE UNIX TIMESTAMP VALUES
echo number_format(strtotime('Today     1:00 a.m.'));
echo PHP_EOL;
echo number_format(strtotime('Yesterday 1:00 a.m.'));
echo PHP_EOL;

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question