Matt Diamond
Matt Diamond

Reputation: 11696

Getting timestamp from relative date/time string with specific timezone AND specific $now value

I'm aware of two ways to get a timestamp from a relative date/time string in PHP:

strtotime: Allows user to specify their own $now value

DateTime object: Allows user to specify their own $timezone value

Is there a way to get a timestamp from a date/time string that allows one to specify both the timezone AND the $now value? It seems like the only way would be to use strtotime while temporarily overriding the default timezone for the entire php application and then setting it back immediately afterwards. That just seems like a hacky solution, and it would be nice if there were a cleaner way.

Edit: there seems to be some misunderstanding about what I'm trying to do. Here's a more concrete example:

"I want to find the timestamp corresponding to the string 'next tuesday at 3:00pm' within the America/Los_Angeles timezone AND specifying an arbitrary value for $now, such as March 14th, 2014 at 8:05am."

Upvotes: 3

Views: 743

Answers (1)

hek2mgl
hek2mgl

Reputation: 157990

I've prepared an example. This may be want you want:

<?php

// Including the timezone int the time strings (thanks @Mike B!!!) 
// will make it very easy.  just strtotime() is required

// create a timestamp for March 14th PDT
$now = strtotime('14 March 2014 8:05am America/Los_Angeles');

// get the requested timestamp.
$nexTuesday = strtotime('next tuesday 3:00 pm America/Los_Angeles', $now);

Upvotes: 2

Related Questions